Hi! Simple question, how do i access a guitext that is attached to a other gameObject than the script. The gameObject name is stored in a variable. Thanks in advance!
Try this
- declare a variable for your object. `var object : GameObject;`
- declare a variable for the GUIText. `var text : GUIText;`
- Find the object you need by using GameObject.Find("thename"). object=GameObject.Find("name");
- Access the GUIText component of this GameObject. You can use GameObject.GetComponent(). `text=object.GetComponent(GUIText);`
- Now you can change or use the text of this GameObject. `var content=text.Text; text.Text="Hello";`
Add a variable to the script that needs to access the guitext. The variable should be public so you can assign the guitext to it via the inspector.
public GUIText referencetotext;
When you have set this variable to the guitext you can modify the guitext from your script by using the referencetotext.
void Start()
{
referencetotext.Text="Hallo";
}
In Javascript the script should look like this
var referencetotext : GUIText;
function Start()
{
referencetotext.Text="Hallo";
}
However I don't have much experience with Javascript so I'm not completely sure.