So I’m new to Unity and I’m trying to instantiate text when another prefab is created.
Think of it as putting a nameplate/healthnumber above a new enemy. However for the text to be diplayed it needs to be a child of a Canvas. How do I get my Canvas so I can implement:
public Text DistanceText;
Text tempTextBox = Instantiate(DistanceText, pos, rot) as Text;
tempTextBox.transform.SetParent(Canvas.transform, false);
I think there should be an easy solution to this but after trying/searching for hours I have no clue how.
Your code is ok, you only need to add the Canvas as a parent:
tempTextBox.transform.SetParent(renderCanvas.transform, false);
Create an empty game object and attach a TextMesh, these do not require a canvas.
then you should be able to do the following.
//Insert the empty game object with the TextMesh attached
public GameObject distanceText;
//Instantiates the Object
GameObject tempTextBox = (GameObject)Instantiate(distanceText, pos, rot);
//Grabs the TextMesh component from the game object
TextMesh theText = tempTextBox.transform.getComponent<TextMesh>();
//Sets the text.
theText.text = "The Text";