- Home /
Access guiText from other object.
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!
Answer by lhk · Nov 01, 2010 at 08:00 AM
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";
Answer by lhk · Oct 31, 2010 at 01:54 PM
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";
}
Thanks for the answer, sure works! Problem is that im using Javascript, is there something familiar like this in JS? Im not that familiar using C, so i can't convert it myself, since i don't understand C that much. Thanks!
basically it is public GUIText referencetotext; function Start () { referencetotext.Text="Hallo"; }
in javascript :) LOL Never knew how learning web scripting and windows program$$anonymous$$g actually made sense :P
Answer by lhk · Oct 31, 2010 at 04:17 PM
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.
Works! Now, i can only change the text that i have choosen, i need to be able to display the text attached to a certain gameObject, depending on wich gameObject is selected. I have the gameObject name stored in a var, i need to access the text attached to that gameObject.
Your answer
Follow this Question
Related Questions
Access variable from script on other gameobject. 1 Answer
Change and Access another scripts Variables 1 Answer
SOLVED - Accessing a C# script value from another GameObject's JavaScript 2 Answers
Quickest method to change a variable on another gameobject. 2 Answers
How do I change the text of a GUIText object through another GameObject using a variable? 2 Answers