Creating GUIText through Script

I’m new to unity and working on a settings page for a basic Falldown-type game. After referencing a few things online, I’m rendering text for a GUI through script by creating a new game object for each one:

void Start()
    {
        GameObject set1 = Instantiate(new GameObject(), guiLocation(1), Quaternion.identity) as GameObject;
        set1.AddComponent("GUIText");
        set1.guiText.text = "Sensitivity";

        GameObject set2 = Instantiate(new GameObject(), guiLocation(2), Quaternion.identity) as GameObject;
        set2.AddComponent("GUIText");
        set2.guiText.text = "Music";

        GameObject set3 = Instantiate(new GameObject(), guiLocation(3), Quaternion.identity) as GameObject;
        set3.AddComponent("GUIText");
        set3.guiText.text = "Sound Effects";
    }

it seems to work well enough, but it seems like there is probably a better way to do it; for some reason, this method creates two gameObjects for each chunk, and I’m having trouble finding out why.

Thank you guys for any help!

Instantiate creates copies of things, typically game objects. These game object can be prefabs, but it can also be an object in the scene. ‘new GameObject()’ creates a new game object in the scene, so your code is first creating a game object in the scene, and then your code is making a copy of that game object.

Typically what you are doing would be handled by a prefab. You would create a prefab that already has a GUIText component. Then all you would have to do is:

GameObject set1 = Instantiate(prefab, guiLocation(1), Quaternion.identity) as GameObject;
set1.guiText = "Sensitivity";

If you really wanted to make one from scratch, consider doing it this way:

GameObject set1 = new GameObject();
set1.transform.position = guiLocation(1);
set1.AddComponent<GUIText>();
set1.guiText = "Sensitivity";

F.Y.I. GUITexts use viewport coordinates which start at (0,0) in the lower left of the screen and go to 1,1 in the upper right.