# Text not instantiating under a canvas (unity)

Below is my code for a text I want to spawn under a canvas. Both spawn and I no longer get errors, but the text isn't spawning under the canvas.

        Instantiate(canvasparent);
Text savescore = Instantiate(score) as Text;
savescore.transform.SetParent(canvasparent.transform, false);


Remember to capture a reference to the instance of the object when you instantiate it, something like this:

Canvas canvasInstance = Instantiate<Canvas>(canvasPrefab);

Text scoreTextInstance = Instantiate<Text>(scoreTextPrefab);

scoreTextInstance.transform.SetParent(canvasInstance.transform, false);


The code you showed is trying to parent the newly-spawned text to the prefab of the canvas, not the instance of the canvas you've spawned into your scene.

This is probably the most common error I see in Unity code, so be very careful to keep track of which variables refer to spawned instances and which to prefabs.

• Right, so i had to just do what i did in the second line in the first, thanks – wfa Sep 4 '16 at 6:30
• but then i get these errors – wfa Sep 4 '16 at 6:33
• Argument #1' cannot convert UnityEngine.GameObject' expression to type UnityEngine.Canvas' – wfa Sep 4 '16 at 6:33
• The best overloaded method match for UnityEngine.Object.Instantiate<UnityEngine.Canvas>(UnityEngine.Canvas)' has some invalid arguments – wfa Sep 4 '16 at 6:33
• Declare canvasPrefab as type Canvas instead of GameObject AlsoscoreTextPrefab  as type Text instead of GameObject. Then assign them again in inspector. – Sourav Paul Sep 4 '16 at 9:48