- Home /
Create GUIText from Javascript
I'm having a hard time finding info on what I thought would be a simple task. Is there a way to create a GUIText object from a script (Javascript)? I don't want to have to create one manually. For what I'm doing thats way to much work as there will be alot of text.
It's ok I figured it out, Like I said, I wanted the GUIText to be created from a script, Not to have to make one myself and add a script to it to control it. In the end, what I really wanted was GUI.Label.
Answer by Arithan · Feb 15, 2013 at 02:49 AM
Information on using GUIText in script can be found in the docs:
http://docs.unity3d.com/Documentation/ScriptReference/GUIText.html
Clicking on the links there will explain how to use each one.
Answer by PAEvenson · Feb 15, 2013 at 02:58 AM
You should just need to add the GUIText Component to a gameobject like so:
var go = Instatiate(new GameObject(), new Vector3(0.5, 0.5, 0.5), Quaternion.identity);
go.AddComponent(GUIText);
go.guiText.text = "Hello World";
If you have never used a GUIText before make sure you take a good look at what the pixel correct does. Ive had a lot of headaches from that alone. Also havent tested the code, but you should get the idea.
It would be better just to use the GameObject constructer with a GUIText component, rather than Instantiate, which is unnecessary.
var go = new GameObject("Text", GUIText);
Answer by lliviu · Aug 23, 2013 at 08:01 AM
PAEvenson solution helped me. Here it is cleaned up of errors and tested in C#:
public void showMessage()
{
GameObject go = Instantiate(new GameObject(), new Vector3(0.5f, 0.5f, 0.5f), Quaternion.identity) as GameObject;
go.AddComponent<GUIText>();
go.guiText.text = "Hello World";
}
Your answer
Follow this Question
Related Questions
changing font + size of text 1 Answer
Gui Button Solid 2 Answers
Why this Error when trying to add a score using the new GUI ? 0 Answers
Restrict characters in GUI.TextField 4 Answers