I’m an absolute beginner to Unity and as a non-native English speaker I don’t fully understand all the errors the console is throwing at me.
I have a scene, in this scene I have two gameobjects.
Object 1 is a Cube with a C# script attached to it.
Object 2 is a 3D Text, this 3D Text has a TextMesh and the text has a value of “Lives:”.
I want to change the text of the TextMesh of object 2, with the script that is attached to object 1.
Now I know that I need to use the following to change the text:
GetComponent(TextMesh).text = "What I want the text to become.";
But I can’t figure out how I access this component from the gameobject.
Here are a few things I tried:
void Start (){
GameObject textObject = GameObject.Find("object2");
textObject.GetComponent(TextMesh).text = "Test";
}
This gives me a whole bunch of errors on the second line, the .text part is also red so I must be accessing it wrong.
void Start ()
{
SpawnBall();
GameObject textObject = GameObject.Find("object2").GetComponent<TextMesh>();
textObject.text = "test";
}
Again, I can’t access .text this way at all.
And I tried at least a dozen of variation of the code above but after an hour of messing with it while looking though other questions that seem the same I have to admit I can’t figure it out and need somebody to spell it out for me.