- Home /
[Solved] UI Text not updating C#
I've been having this problem for a while now but it only seems to happen in this project. When I try and use the Text.text function in Unity it doesn't update the text. However, after I stop the game and play it again then the text I am trying to change changes to what I was trying to change before. For instance, if I am updating the score it won't register the update in the text until I restart. Here are a couple of scripts that aren't working.
Have you checked to see if you are changing a prefab ins$$anonymous$$d of the object in the hierarchy?
I'm going to go with @veugeljame on this. The behavior you are describing literally sounds like you are altering the prefab of an object, and not the instance of the prefab.
I just checked and I am changing the prefab... how do I ins$$anonymous$$d change the object in the hierarchy?
Drag the prefab out of your project window into the heirarchy. You should now have a blue object in the heirarchy which is a copy of your prefab. Use that in your game ins$$anonymous$$d of the prefab.
Answer by James2Games · Feb 23, 2017 at 12:09 AM
Have you checked to see if you are changing a prefab instead of the object in the hierarchy?
If so then you can drag the prefab out of your project window into the heirarchy. You should now have a blue object in the heirarchy which is a copy of your prefab. Use that in your game instead of the prefab.
god praise this comment...couldn't figure out the problem with clearing an Inputfield.text and changing its value until i read this
I didn't understand what this guy is talking about, and why he gets so much upvote. Until I found that I didn't use the result returned by Instantiate(), but change the prefab itself! That's exactly what I'm doing! And the text always show the last text I 'm assigning to it!
Answer by Thorny2000 · Feb 22, 2017 at 05:36 AM
Delete the "public" in "public Text GoldText;". Be very careful with public variables, can lead to very hard to find bugs where you think you are assigning a value in code but you also changed it in the inspector and it is serialised. I wish Unity didn't use public to do this!
Note you are assigning GoldText with the line "GoldText = GetComponent ();". This is fine if this script is actually on your Text object in the inspector. If it isn't then this is your bug.
Note both of the above are assigning GoldText with values, one of them is very probably your bug.
That's not really true, public variables cannot lead to "hard to find bugs", but if his Text is not ACTUALLY a component of the main object, then it will set it to null due to not finding it, however he is not saying he is getting errors, which would of immediately occured at the GoldText.text = "25";
if it was null. For the most part, public/private are just accessors for the developer to restrict their own access.
Sorry about that! Forgot to mention that I am getting errors. "NullReferenceException: Object reference not set to an instance of an object" on the line with GoldText.text = "25"
If I'm not assigning GoldText in the Inspector, how should I assign it?
You have to do 1 or the other. If you set it in the inspector, then you shouldn't use the GetComponent<Text>()
. If you want to use GetComponent, then it has to be the object the script is attached to, or the only Text object as a child, then you could GetComponentInChildren<Text>()
I wasn't clear, let me explain: You make a public var and assign it a default value in the script (e.g public var speed = 20.0f). Then you modify the value in the inspector, time passes, you forget you modified the value in the inspector. You go back to your script one day, you look at the script and see the value you assigned it in the script that looks correct, forgetting you changed the value in the inspector. Yet you can set a breakpoint your script and see the value is wrong at runtime. You edit the assigned value in the script to besure (speed = 10.0f) and yet it makes no difference when you run your app (speed isn't 10 when at runtime). You forget the value is serialized and being overwritten by the value you set in the inspector.
I agree with G$$anonymous$$T that if a variable doesn't need to be public then it shouldn't be.
Answer by BlockFade · Feb 22, 2017 at 06:27 PM
Have you assigned the Text variable in the Inspector? If so, then are you using TextVariable.text = "Your string here";
?
It's always helpful if you actually read the script before answering. If you did you would know I was doing all of those things.