- Home /
Can't change the 'text' of a 3DText object through a script, no matter what I do.
What's the appropriate way to change the text of a 3DText object through script? I've looked and looked and tried 3 or 4 different ways, but none of them work.
Answer by BiG · Nov 30, 2012 at 02:20 PM
It should work this way:
GetComponent.<TextMesh>().text = "Your text";
In javascript? Where would I put that? Because I get 'NullReferenceException: Object reference not set to an instance of an object' with that.
In Javascript you shouldn't really use the generic form of GetComponent (it's very slightly slower, and is more awkward).
GetComponent(Text$$anonymous$$esh).text = "Your text";
If you get a null reference exception then you have no Text$$anonymous$$esh component attached.
@Erich5h5: Thanks for the clarification. I'm a bit confused, because I've seen these two other questions, regarding the topic:
http://answers.unity3d.com/questions/52323/changing-text-text-value-within-3d-text.html
http://answers.unity3d.com/questions/46382/change-a-3d-text-at-run-time.html
The answer at the second one is of yours, but maybe you was intending C#, there. Thanks, by the way.
That was from almost 2 years ago...at the time, the non-generic GetComponent returned Component, rather than the type. (Regarding Unityscript anyway...C# still returns Component with the non-generic version.) But thanks for pointing that out, I'll edit it. :)