Noob question - Updating Text in Canvas

Hi, all!
I have a ridiculous problem with Unity's UI:
In my game I want to display the name of the active vehicle on the screen and update it whenever the vehicle changes. So:
1 - I add a Canvas to the project.
2 - I add a Text component called IDC_NAME to Canvas.
3 - I add a script called Driver to the vehicle's main body.
4 - In Driver's Update method I add : IDC_NAME.text = "Whatever";

And Unity tells me that IDC_NAME doesn't exist.
Can you help? Sorry for being so dumb.

PS: I didn't forget "Using UnityEngine.UI".

you need to get a reference to your text object that is on your canvas, as it doesnt exist in the vehicle script, yet. Or, adversely, get a reference to your vehicle/s within the canvas / text script (if one exists) , but yea, this is just a simple case of no references.

Thanks to Homicide I have made some progress, I hope, but not enough.
Now the Canvas is a component of an ampty Game Object called "Game Object" and IDC_NAME is also a component of the same Game Object. And my script "SetCanvas" is a third component of the same (Screen1.jpg). And C# looks happy with that (Screen2.jpg). Yet the thing doesn't work and the Console says "NullReferenceException: Object reference (IDC_NAME, obviously) not set to an instance of an object."

Something very basic escapes me in the structure of this project (my first with Unity's UI) and I'm still lost.... 9475786--1332094--Screen1.jpg 9475786--1332097--Screen2.jpg

You would likely be using textmesh pro. However. Yes. You declared a variable but you never told it what text needed to be in it. It either needs to be a public variable and you assign it in the inspector or you need to use code to go find it

You are perfectly right, bugfinders. But, as you can see in Screen3.jpg, I did define IDC_NAME in the inspector, and I gave it a default value. And it is a component of both Canvas and Game Manager*.* And the script SetCanvas is also a component of Game Manager And my question now is : what code should I use to change IDC_NAME's text from SetCanvas ?

Of course, I can switch to TextMesh Pro. No problem about that. Will it help?


nope.. you named a game object in the inspector IDC_NAME, just cos you do doesnt mean you can just type IDC_NAME anywhere in code and make it work - if you think of code for all the other things where theres getcomponent, or whatever, chances are the simplest is you need a public variable - whatever you call it is just that, then you can drag the heirachy object into that.. this is kinda normal basicy stuff, i'd suggest having a watch of a few youtube videos, they will help a lot and far better than us all type random code at you that you dont understand why

Got it! You see, I am used to standard C#, not to Unity's dialect, and I hadn't thought of that. Besides, I said that I'm dumb. All the same, thanks for your unpleasant and contemptuous help.

i wasnt being rude but if you wish to take it as so, thats on you. Im not responsible for your bad mood, you chose it.

1 Like