- Home /
Changing TextMesh text via script
Been banging at this for awhile and its time to ask for help. In a script attached to GameObject A, I'm trying to update the text of a TextMesh attached to a child object of GameObject B.
Various iterations all have resulted in syntax or object expression/conversion errors; I currently have (in script on GO A)
TextMesh theText = transform_GO_B.Find("myMesh").GetComponent(TextMesh);
theText.text = "booyah!";
which is not valid/working. How do I form the reference to child object/Text Mesh on GameObject B from GameObject A?
Answer by robhuhn · Jul 31, 2013 at 07:43 AM
This should throw an exception "Expression denotes a type', where a
variable', value' or
method group' was expected"
Type
TextMesh theText = (TextMesh) transform_GO_B.Find("myMesh").GetComponent(typeof(TextMesh))
or with generics
TextMesh theText = transform_GO_B.Find("myMesh").GetComponent<TextMesh>();
myMesh has to be a child of transform_GO_B then.
thanks both for feedback; Robhuhn your syntax worked nicely
I toght you are working with js.
If I'm not wrong, your original code should work in jr.
That's why I was thinking your problem was the find....
Next time, please state what language you are working with :)
Will do, new to the board and forgot to specify language of C#
Answer by dorpeleg · Jul 31, 2013 at 05:59 AM
I Think that if you use GameObject.Find instead of Transfom.Find, it will work.
But you can also try finding the child by using Transform.Find or Transform.GetChild.
Notice the path in Transform.Find.
Your answer
![](https://koobas.hobune.stream/wayback/20220613111240im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Making a 3d text object represent the "health" of an object? 1 Answer
GetComponent(NameOfScript) error "reference not set to an instance" 1 Answer
Accessing and using another objects script variable 1 Answer
How to distinguish between multiple components of same type 5 Answers
Script Referencing 2 Answers