- Home /
What is needed for a GUIText component?
I'm trying to, from a script added to an otherwise empty object, create a GUIText component, and write text to it.
I've got the AddComponent working fine, but that's about as far as i've gotten. After that i've been hitting play and experimenting with things in the inspector, to figure out what i have to do.
So far i've tried typing "test" in the text field, assigning FontMaterial to the material slot, assigning arial as the font, setting the font size to 10, setting the anchor to middle center, setting the pixel offset to zero, and setting the font colour to black
I would think that all of these things would be enough to get things going, and m,ake the word "test" appear in the middle of the screen in black letters , but i still cannot make any text appear onscreen.
any idea what i might be missing? What steps are needed to make this work?
Answer by Vurawid · Dec 07, 2014 at 05:25 AM
The GUIText component uses viewport space and the initial position usually puts it offscreen. Doing the following may help to get started and better understand.
Create an empty GameObject
Add a GUIText component to it
Set the X to 0.5
Set the Y to 0.5
Change the text to "test"
Switch to the game view window
Here is a video from the Unity Space Shooter tutorial that I found useful to learn more about GUIText... Space Shooter Project: Counting Points and Displaying the Score
Edit: Adding some code that creates a GUIText component in center of view with text "Hello World".
GameObject myGameObject = new GameObject ();
myGameObject.transform.position = new Vector3(0.5f, 0.5f, 0f);
GUIText myComponent = myGameObject.AddComponent<GUIText> ();
myComponent.text = "Hello World";
thank you my friend <3 i figured ou the problem of the XY position shortly after posting this, but i was still stuck on the subsequent requirement that i couldn't actually then have the text component on the object itself.
i've never actually created gameobjects in code like that,forgot it could be done. Thank you, this will work well.
Your answer
Follow this Question
Related Questions
GUIText position changes with changing resolution 2 Answers
Add text on top of a gameobject. What is the best way? 1 Answer
GUItext works 1 of 3 situations? 0 Answers
Draw GuiText on 2d tile sprite Like 2048 game 0 Answers
Display GUI Text 1 Answer