- Home /
Problem with displaying text
Hi guys! So, I'm doing some Unity practice before I return to college and I was just working with music and having text that shows the song name and pitch. I have the song name and the pitch (which can be changed with two in-game buttons) visible for one of the songs ('My Shot') but it doesn't work for the other one ('Wait For It', yes I'm Hamilton trash), the name appears fine, but the pitch only briefly flashes onscreen. If anyone could help with this I would appreciate it! Here are the two scripts that it's handled in:
Switching songs:
void Start () {
myShot = true;
waitForIt = false;
}
void Update()
{
if (myShot == true)
{
//main.Pause();
main.clip = Shot;
myShot = false;
}
if (waitForIt == true)
{
//main.Pause();
main.clip = Wait;
main.Play();
waitForIt = false;
}
}
// Update is called once per frame
void OnTriggerEnter2D (Collider2D other) {
if(other.tag == "Song")
{
myShot = false;
waitForIt = true;
}
}
}
Text stuff:
void Start()
{
pitch = main.pitch;
}
void Update()
{
songText.text = song + " " + "Pitch:" + pitch;
pitch = main.pitch;
if (SongSwitch.myShot == true)
{
song = "My Shot";
}
if (SongSwitch.waitForIt == true)
{
song = "Wait For It";
}
}
}
maybe it just does not fit the text field. What happens if you show only the pitch?
songText.text = pitch;
I tried what you said and the pitch did show up, but then I tried making the text field bigger and it didn't work. Since posting this question, I've put in a button to return the pitch to 1, and noticed that whenever the pitch is at 1, it shows alongside the song name, but then when I change it, that causes it to flash.
Answer by LCStark · Sep 17, 2018 at 12:15 PM
Like hexagonius said, your text is probably too long for the Text
element to display properly. To check that, select your text object, find the Horizontal Overflow
field in the Inspector (it should be set to Wrap
) and set it to Overflow
. You will see that even if the text is too long, it will be displayed outside of the text area. You might also want to change the Vertical Overflow
, if needed.
The second problem is the way you are displaying the pitch value. It is a float
value, so when you simply add it to the string, it displays it precisely, down to several decimal places. Depending on the value of the pitch
field, the decimal places number might change, causing the text to flicker if it can't fit in the box. You might want to control the number of decimal places yourself. Instead of "Pitch:" + pitch;
try using "Pitch:" + pitch.ToString("F2");
. That will limit the amount of decimals to 2.
This worked, thanks! Also, thanks for the tip about limiting the amount of decimals, that'll probably be helpful in later projects as well.
Your answer
Follow this Question
Related Questions
How would I make 1 bool true whilst making others false 1 Answer
How to switch between only the weapons that the player currently has 1 Answer
problem on switching gun (a var don't turn true) 0 Answers
Sound volume and pitch based on speed of rigidbody and/or normal object? 1 Answer
How to get a rigidbody's Local Angular Velocity (Yaw, Pitch & Roll) ? 1 Answer