Instantiate prefab - how to get prefab to be parented in canvas

Hi,
I’m new to Unity and I’m trying to use code from Unity docs to instantiate a prefab in my scene:

Problem is the prefab is invisible unless parented in the canvas. Unfortunately Unity docs doesn’t show how to make a prefab that is actually parented in the canvas and therefore visible …

Can somebody help me? I need the prefab to be visible instead of invisible. Thanks!

Not sure if this is necessary, but the name of my Canvas is myCanvas.
Here is my code:

using UnityEngine;
using System.Collections;

public class CloneManager : MonoBehaviour {

public GameObject prefab;

void Start()
{
Instantiate (prefab, new Vector3 (0, 0, 0), Quaternion.identity);
}
}

You could do something like this:

Vector3 position = Vector3.zero; 
Transform newPrefabInstance = (Transform)Instantiate(prefab, position, Quaternion.identity);
if (newPrefabInstance != null)
{
    newPrefabInstance.transform.parent = parentGameObject.transform;
}

This assumes your Canvas is attached to the parentGameObject

SetParent()

@sstrong : hmmm… I’m new so here’s my best guess based on your post… I created a game object called “parentGameObject”. Then I tried using your code like this.

using UnityEngine;
using System.Collections;

public class GameObjectScript : MonoBehaviour {

public GameObject prefab;

void Start()
{
Vector3 position = Vector3.zero;
Transform newPrefabInstance = (Transform)Instantiate (prefab, position, Quaternion.identity);

if (newPrefabInstance != null) {
newPrefabInstance.transform.parent = parentGameObject.transform;
}
}
}

and I got this error:
the name ‘parentGameObject’ does not exist in this context.

hmmm… again I’m new to Unity so not really great at filling in the blanks … detailed explanations might work best for me

Thanks for sending this link. After reading the article, I was able to get this far:

using UnityEngine;
using System.Collections;

public class CloneManager : MonoBehaviour {

public GameObject prefab;

void Start()
{
Instantiate(prefab, new Vector3(0, 0, 0), Quaternion.identity);
prefab.transform.SetParent (myCanvas, false);
}

}

This results in the following error:
"the name myCanvas does not exist in the current context’
In this case, I have named my Canvas in the hierarchy to “myCanvas”. Not sure how to proceed. I’m new so your help make this work would be appreciated.

using UnityEngine;
using System.Collections;

public class GameObjectScript : MonoBehaviour {

public GameObject prefab;
public GameObject parentGameObject; // Drag your canvas gameobject here

void Start()
{
  Vector3 position = Vector3.zero;

  Transform newPrefabInstance = (Transform)Instantiate (prefab, position, Quaternion.identity);

  if (newPrefabInstance != null) {
    newPrefabInstance.parent = parentGameObject.transform;
  }
}

}

@sstrong : Thank you. Hmm… Now I’m getting this error: 'Invalid cast exception. Cannot cast from source type to destination type. GameObjectsScript.Start() (at Assets/GameObjectScript.cs:13)".

    public GameObject prefab;
    public GameObject parentGameObject; // Drag your canvas gameobject here

    // Use this for initialization
    void Start()
    {
        Vector3 position = Vector3.zero;

        GameObject newPrefabInstance = (GameObject)Instantiate(prefab, position, Quaternion.identity);
        if (newPrefabInstance != null)
        {
            newPrefabInstance.transform.parent = parentGameObject.transform;
        }

    }

This worked for me. thank you :slight_smile: I really appreciate it.

Hmm… For some reason the game object will always instantiate at the bottom left corner – instead of center screen for example.

I noticed in Canvas > Rect transform there is grayed-out text that says “Some values driven by Canvas”. And there are some values pre-populated which are putting the object in the bottom left corner. When I run the game and manually update those values to Pos X = 0 and Pos Y = 0 then the object appears in center screen. But I was hoping to do this in the code.

Maybe I’m overlooking something… I wonder if there is a way to fix this?

There are a bunch of Unity tutorials on how the canvas and rect transforms work. If you walk through that list of tutorials you should get a much better idea of how things are positioned in the UI.
https://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-canvas