Unity changing text with script c# - c#

I'm trying to add a scoring method in my Unity phone game by changing the UI text.
Here is My code:
using UnityEngine;
using UnityEngine.UI;
public class ScoreDisplay : MonoBehaviour
{
// Start is called before the first frame update
[SerializeField]private Text m_MyText;
void Start()
{
m_MyText.text = "This is my text";
}
// Update is called once per frame
void Update()
{
m_MyText.text = "UWU";
}
}
Nothing pops up when I search for a text object:
And I cannot drag the mesh text into the public variable. Any help would be appreciated, thank you.

Text is a legacy element and is mostly unused. Instead, you want Text Mesh Pro's text component which is under the TMPro namespace. The componant is called TextMeshProUGUI.
Replace
[SerializeField]private Text m_MyText;
with
[SerializeField]private TMPro.TextMeshProUGUI m_MyText;
The TextMeshProUGUI componant has a text field, so your code should work as normal after replacing the line.

Related

enable/disable Objects in Unity

I am totally new in programmation and unity, so I have hard time with basically everything!
Here is my issue : I have a 2D static game with a grid of boxes. each box is made of buttons to click.
I want all the boxes but one not visible at the beginning, and then the box have a button to make boxes appears one by one.
here is my code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OpenBox : MonoBehaviour
{
// Start is called before the first frame update
private GameObject boite1;
void Start()
{
box1 = GetComponent<Box1> ();
}
void Update()
{
if (Input.GetKeyUp(KeyCode.Space))
{
box1.enabled = true;
}
}
}
The "Box1" is underline in red with message : CS0246, The type or namespace name could not be found.
I am not sure I know how to refer to the game object.
thank you for your help !
try use
gameObject.SetActive(false);
more detail unity doc

Can I use a variable I got from the player into a text box?

I'm trying to make an app in Unity which requires your name and other details. I'm using the legacy Input Field to get the answers from the player which worked, but I can't seem to use the Input in any way other than Debug.Log.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ReadInput : MonoBehaviour
{
private string input;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void ReadStringInput(string s)
{
input = s;
Debug.Log(input);
}
}
I used this simple script to get the input but I have no clue how to actually use it in the game. It want it to say, for example, Welcome,(Person's Name/Input) outside of the log and actually into a text object.
If you right click in the hierarchy, under UI you can add a text object. I like to use a free package called Text Mesh Pro, which you can get in the asset store, but you don't need to.
Once you've made your text object, it should appear as a child of a gameobject called the Canvas. The text object should have a text field that you can access from script.
Text mytext;
public void UpdateMyText(string s)
{
mytext.text = s;
}
Here's the documentation
Just drag the text object into the script's mytext field in the inspector, and call that function when you want to update your text.

FMOD parameter not changing through C# script

I'm currently trying to develop my first mini game (a VR musical experience).
I'm trying to have the value of a slider control an FMOD parameter, but nothing happens. Plus the function does not show in the On Value Changed of the slider...
I'm already using that slider to control the transparency of a material and if I try changing the FMOD parameter manually in the inspector it works just fine.
Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FMODParameters : MonoBehaviour
{
FMODUnity.StudioEventEmitter changeVolume;
FMOD.Studio.EventInstance bass;
public string bassVolumeParameter;
public Slider slider;
void Start()
{
changeVolume = GetComponent<FMODUnity.StudioEventEmitter>();
bass = FMODUnity.RuntimeManager.CreateInstance("event:/Beat/2D/2D Bass");
slider = GetComponent<Slider>();
}
// Update is called once per frame
void Update()
{
SetBassVolume();
}
void SetBassVolume()
{
bass.setParameterByName(bassVolumeParameter, slider.value / 250);
}
}
Thanks in advance for the help! :)
The problem is that you have to add the function in the onValueChanged () listener.
To do it:
Select the gameObject with the Slider component;
Click the "+" button in the OnValueChanged () listener below;
In the empty field, under "Runtime only", drag the gameObject with
this script;
Click the box that now has the name "No Function";
In the component list, place the cursor over the script name, and
under the "Dynamic parameters" category, the SetBassVolume () method.
But first you should tweak the method a bit, like so:
void SetBassVolume(float _value)
{
bass.setParameterByName(bassVolumeParameter, _value / 250);
}
Good work!

Set Text with textmesh pro

Is there any way to cycle text with textmesh script?
I'm interested in cycling text so i can display different text on my 2D game.
I have try the script on this website. But I don't think it works. Maybe I'm doing something wrong?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class CyclingText : MonoBehaviour
{
public TMP_Text text;
void Update()
{
TextMeshPro textmeshPro = GetComponent<TextMeshPro>();
textmeshPro.SetText("The first number is {0} and the 2nd is {1:2} and the 3rd is {3:0}.", 4, 6.345f, 3.5f);
// The text displayed will be:
// The first number is 4 and the 2nd is 6.35 and the 3rd is 4.
}
}
Can someone provide me with an answer or explained what I done wrong?
You seem a bit confused about the use of TMP
1
If you set the TMP in the inspector as public you can simply
public TMP_text text;
And then simply drag the text inside the inspector and simply:
text.text = "Text displayed";
2
If you don't create a public variable but a private, you can:
private TMP_text text;
And then, if you script is in the text object you can:
text = this.gameObject.GetCOmponent<TextMeshPro>();
And
text.text = "Your text";

How to make score appear on GUI?

I'm making a game, and I'm having trouble making the score appear on the game.
So far, this is all I have:
public class keepingScore : MonoBehaviour {
public static double homeScore;
// Use this for initialization
void Start ()
{
double homeScore = 5.0;
print(homeScore);
}
}
So my code is printing 5 to the console, and when I've tried other methods, it says it wont work because homeScore is not a string.
Any help guys?
Thanks!
Please try use GUI:
https://docs.unity3d.com/ScriptReference/GUI.html
or you can try Canvas GUI that must better:
https://docs.unity3d.com/Manual/UICanvas.html
So first of all what you need if you want to have score in your GUI is to have a Text component in your scene.
Once you have your Text component in your scene you need to create a script that will handle the score and add it to the Text component you have created. This is an example of a Score manager script:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class keepingScore : MonoBehaviour {
public static double homeScore;
Text text;
void Awake () {
text = GetComponent<Text>();
homeScore = 0.0;
}
// Update is called once per frame
void Update () {
text.text = "Score: " + homeScore;
}
}
Now you can attach this script to the Text component you have created earlier. What this script does is first retreive the Text component that it is attached too and initialize a public static double homeScore that you can access and modify from any script by just doing keepingScore.homeScore. Finally the Update function will run every frame to update the Text component you have.
Now that you have a Text component in your scene with this script attached to it, you can start modifying the value of your score. From wherever you want. An example would be let's say when your player picks up a coin you want to give him 1 point, so if the player collides with the coin you add 1 to the homeScore
void OnCollisionEnter(Collision collision) {
if (collision.CompareTag("Coin"))
keepingScore.homeScore++;
}
This would for example add 1 to the score when the player collides with the coin.
You can do keepingScore.homeScore += pointAmount wherever you want to add points to the player and it will automatically update the GUI Text.

Categories