generate GUIText at runtime C#

Hello,

I want automaticly generate with my script a GUIText.
At the moment i have a script that work, but you need to add a GuiText with drag and drop at the Inspector.

THE SCRIPT:

using UnityEngine;
using System.Collections;

public class hover : MonoBehaviour
{
		public Vector3 offset;    
		Camera cam;
		public string mytext;
		public GUIText myGuiText;

		void Start ()
		{
				myGuiText.text = mytext;
				myGuiText.enabled = false;
				cam = Camera.main;
		}

		void Update ()
		{
				myGuiText.transform.position = cam.WorldToViewportPoint (transform.position + offset);
		}

		void OnMouseEnter ()
		{
				myGuiText.enabled = true;
		}

		void OnMouseExit ()
		{
				myGuiText.enabled = false;
		}

}

I need this script to run without using “public GUIText myGuiText”.

Sry if there is somewhere a solution… I found a lot of similar ones. But I dont get it to fix, so it work with my script.

thy for help… I hope.

You can use AddComponent(). This code creates a new game object, add a GUIText component, centers the text on the screen (because GUIText uses Viewport coordiantes), and sets the text:

#pragma strict

function Start() {
	var go = new GameObject();
	go.AddComponent(GUIText);
	go.transform.position = Vector3(0.5,0.5,0.0);
	go.guiText.text = "Hello World";
}

You can add the component to an existing empty game object, but remember that GUITexts use Viewport coordinates, not world coordinates.

private bool displayLabel;

void OnMouseEnter(){
    displayLabel = true;
}
void OnMouseExit(){
    displayLabel = false;
}

void OnGUI(){
    if(displayLabel){
        GUI.Label(new Rect(10, 10, 100, 20), "Hello World!");
    }
}

Alternatively:

			GameObject g = new GameObject();
			Canvas canvas = g.AddComponent<Canvas>();
			canvas.renderMode = RenderMode.WorldSpace;
			CanvasScaler cs = g.AddComponent<CanvasScaler>();
			cs.scaleFactor = 10.0f;
			cs.dynamicPixelsPerUnit = 10f;
			GraphicRaycaster gr = g.AddComponent<GraphicRaycaster>();
			g.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 3.0f);
			g.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 3.0f);
			GameObject g2 = new GameObject();
			g2.name = "Text";
			g2.transform.parent = g.transform;
			Text t = g2.AddComponent<Text>();
			g2.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 3.0f);
			g2.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 3.0f);
			t.alignment = TextAnchor.MiddleCenter;
			t.horizontalOverflow = HorizontalWrapMode.Overflow;
			t.verticalOverflow = VerticalWrapMode.Overflow;
			Font ArialFont = (Font)Resources.GetBuiltinResource (typeof(Font), "Arial.ttf");
			t.font = ArialFont;
			t.fontSize = 7;
			t.text = "Test";
			t.enabled = true;
			t.color = Color.black;

            g.name = "Text Label";
            bool bWorldPosition = false;

			g.GetComponent<RectTransform>().SetParent(this.transform, bWorldPosition);
            g.transform.localPosition = new Vector3(0f, fTextLabelHeight, 0f);
            g.transform.localScale = new Vector3( 
                                                 1.0f / this.transform.localScale.x * 0.1f,
                                                 1.0f / this.transform.localScale.y * 0.1f, 
                                                 1.0f / this.transform.localScale.z * 0.1f );