- Home /
Simple question about Guitext
I've set up a GUItext instance to display on the screen, and was trying to drag and drop it on to a prefab that requires access to it to alter the text property. It won't let me do this, and clicking on the rollout button next to the public variable "score"(which I declared as a public GUIText variable in my script) in the explorer shows my assets folder in the "select guitext" window as being empty. Just wondering if I'm going about this the right way, is it possible to use a guitext variable in this way, or is there a better way to do it?
I'm new to Unity and was going thru the beginner tutorials and had this same issue when it came to adding the "Count" of cubes. When I tried to drag the GUIText object I had created from the Hierarchy tab into the inspector and onto the controller script component field box it did not work. I'm going to try going to my script and changing the public variable to accept a GameObject ins$$anonymous$$d and then cast it as a GUIText internal to my script to use it. I would like to know if it is a problem with the designer or the programmer (me) though, because I'm learning.
BTW, I'm a pretty accomplished c# developer professionally, so don't spend extra time dumbing down the answer unless you feel the community would benefit.
Answer by dannyskim · Jan 10, 2012 at 05:12 PM
Hmm. I'm able to set a GUIText as a public variable in the inspector no problem, whether it just be plainly in the scene or setting it myself through code.
You may want to consider trying making the Score part of a prefab first, and then seeing if you can select it. Access to some objects requires that they be a prefab before they're assigned as variables in the inspector.
Thanks for the suggestion, I tried making it a prefab, but no dice. I could drag the prefab on to the public variable, but not the instance of the GUItext I wanted to refer to.
I'm not quite sure why this is happening, I'll see if I can replicate the issue throughout my work today. $$anonymous$$y only other suggestion as a workaround would be to create a public static text variable on the object you were originally wanting to attach the GUIText to, and then create another script on the actual GUIText item to access and set the static variable accordingly to the GUIText.
Or you can try doing a GameObject.Find() during runtime and storing it as a private static variable. I know doing a GameObject.Find() will absolutely allow you to store the variable in your script.
So for example:
guiTextInstance = GameObject.Find( "GuiTextNameHere" ).GetComponent(GUIText)();
That's for c#, and I believe the syntax is correct. I haven't tried it myself. The parentheses surrounding the GUIText word actually need to be less than / greater than signs.
Your answer
Follow this Question
Related Questions
Display controls in-line with text? 0 Answers
Written objectives in unity 3D 0 Answers
Change text value 3 Answers
Updating GUI text 0 Answers
Display a block of text to the screen 0 Answers