- Home /
Gameobject: 3D Text, how to change text?
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.
Answer by koray1396 · Apr 22, 2014 at 04:20 PM
Should be;
void Start ()
{
SpawnBall();
TextMesh textObject = GameObject.Find("object2").GetComponent<TextMesh>();
textObject.text = "test";
}
Answer by Enumerator_T · Nov 05, 2014 at 08:16 PM
//Inside your class:
public TextMesh[] TEXTMESHES = new TextMesh[0];
//Change the size of TEXTMESHES in the Inspector Panel to something higher than zero
//Asign the 3DText Object to the script element list & Call with the following:
TEXTMESHES[0].text = "lol";
Your answer
Follow this Question
Related Questions
Insert 3d text to the front face of a cube GameObject 0 Answers
Display Name Above Object 2 Answers
Making a Roadsign?(or : help on how to use textMeshes) 3 Answers
have my keys' index on top of them in 3D text 1 Answer
Text on Models 1 Answer