Dynamically create GUIText from script.

how can I create GUIText from script.. Below is the script that I am trying to do. But there has an error "Null Reference" in accessing to btnTexts...

private GUIText[] btnTexts;
public  Font font;

public void CreateTexts()

btnTexts = new GUIText[numButtons];

for (int i = 0; i < numButtons; i++)

//Create a Text
            btnTexts *= new GUIText();*
 _btnTexts*.font = font;*_
 <em>_btnTexts*.transform.position = new Vector3(0.0f, startPosY, 2.0f);*_</em>
 <em><em>_btnTexts*.text = "Test";*_</em></em>
<em><em>_*<p>The above code is not working and always saying NullReference Exception... :(</p>*_</em></em>

You'll need to use AddComponent instead of new GUIText to create the GUIText - GUIText components don't work without a gameobject

Something like this should do it:

//Create a Text
GameObject go = new GameObject("GUIText " + i);
btnTexts *= (GUIText)go.AddComponent(typeof(GUIText));*
_btnTexts*.font = font;*_

Sry,,, I Confused between JavaScript and C# Style.. It works like that

GameObject go = new GameObject("GUIText " + i); btnTexts *= go.AddComponent();

* *```* _btnTexts*.font = font;*_ _btnTexts*.material.color = new Color(1.0f, 1.0f, 1.0f);*_ _btnTexts*.transform.position = new Vector3(0.0f, startPosY, 10.0f);*_ _btnTexts*.text = "Test";*_ _*```*_