- Home /
Text UI not updating
Hi! I'm new to Unity and C# and working on a project. I want to make an on-screen timer tick down as the player moves, but for some reason the text on screen isn't updating. Here's the script I've put on the text object, which I have as the child of a UI canvas:
using UnityEngine;
using UnityEngine.UI;
public class MovementUI : MonoBehaviour
{
//Script changes UI text to display value of MovementTimer.movementTimer
float movementMeter;
Text display;
// Start is called before the first frame update
void Start()
{
display = GetComponent<Text>();
movementMeter = GameObject.Find("PlayerCircle").GetComponent<MovementTimer>().movementTimer;
Debug.Log(movementMeter);
}
// Update is called once per frame
void Update()
{
display.text = "" + movementMeter;
}
}
You can see that the float movementMeter is initialized to a float from another script. This is working correctly, Debug.Log(movementMeter) in the Start function prints the correct number to the console. I'm guessing it's a problem with how I'm using display.text, but I'm not sure.
Thank you!
Answer by k234234w · Jul 17, 2019 at 06:23 PM
movementMeter will get set once in the start, I am not seeing where you are having it tick down? Is the number you are seeing displayed different from what is logged in start? From what I am seeing your are not altering movementMeter so it will be the same every frame, and thus the text will not change. Initializing it to the float from the other script doesnt mean every time it changes in the other script you will see it in your update change. You have a local float variable, you are setting its value once and only once.
You could get a reference to $$anonymous$$ovementTimer, cache it. And if you make $$anonymous$$ovementTimers movementTimer public, you could set your text to that in Update.
public class $$anonymous$$ovementUI : $$anonymous$$onoBehaviour
{
//Script changes UI text to display value of $$anonymous$$ovementTimer.movementTimer
private $$anonymous$$ovementTimer movementTimer = null;
Text display;
private void Awake()
{
movementTimer = GameObject.Find("PlayerCircle").GetComponent<$$anonymous$$ovementTimer>();
}
// Start is called before the first frame update
void Start()
{
display = GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
// Assu$$anonymous$$g its a public variable.
display.text = "" + movementTimer.movementTimer;
}
}
You're right, I guess I assumed movement$$anonymous$$eter would update alongside $$anonymous$$ovementTimer.movementTimer. I moved the line where I define movement$$anonymous$$eter from Start() to Update(), and it works fine now. Thank you!
It would be better to not have to do the GameObject.Find every update frame. Why can't you just get a reference to $$anonymous$$ovementTimer in Awake, and then in your update just set the text to the cached reference to $$anonymous$$ovementTimers movementTimer variable.
Try out my code ins$$anonymous$$d of using GameObject.Find every update frame.
Answer by KloverGames · Jul 17, 2019 at 06:29 PM
Make sure you have an event system in your hierarchy, I know you need it for buttons but maybe it's for all ui updates
Your answer
Follow this Question
Related Questions
Text Not updating 1 Answer
uGUI Text not updated until RectTransform has been altered 1 Answer
Text component changes the text but doesn't remove the previous version. 0 Answers
Multiple gui instantiation issues 0 Answers
How to add string once to the UI text, but update the substring every frame in unity? 0 Answers