1
\$\begingroup\$

I am trying to instantiate a Prefab inside a Canvas. Right now, it keeps appearing behind the canvas. I was reading online and it said to set the parent of the GameObject you are trying to instantiate. However, I can't figure out how to get the Canvas component.

My hierarchy looks like this:

enter image description here

My EnemyFormation object has this script attached to it:

public class EnemySpawner : MonoBehaviour {

    public GameObject enemyPrefab;

    void Start () {
        Instantiate(enemyPrefab, new Vector3(0, 0, 0), Quaternion.identity);
        Debug.Log("Spawning enemy");
    }
}

I attach the prefab inside Unity editor. I tried adding this line under Instantiate():

enemyPrefab.transform.SetParent(GameObject.FindGameObjectWithTag("Canvas"), false);

However, I get an error saying it can't convert from Unity.GameObject to Unity.Transform.

How do I make the prefab appear inside the Canvas instead of behind it? Thanks in advance!

This is what it looks like when it creates it in-game: Enemy_Orange3(Clone) is created outside of the canvas and at the bottom left instead of the center. My camera is in the same position and is the same size of the canvas.

enter image description here

\$\endgroup\$
1
\$\begingroup\$

SetParent like below. You have to use a another GameObject to instantiate and set the parent.

GameObject enemy = Instantiate(enemyPrefab, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
enemy.transform.SetParent (GameObject.FindGameObjectWithTag("Canvas").transform, false);

Setting parent is done with transforms of two objects.

\$\endgroup\$
  • \$\begingroup\$ It still creates the object outside of the canvas Enemy_Orange3(Clone): zippy.gfycat.com/MemorableWaterloggedCoelacanth.gif. My SetParent() line looks exactly like yours. \$\endgroup\$ – o.o Aug 9 '16 at 4:09
  • \$\begingroup\$ Did you tag the canvas? Is your enemyPrefab an UI object? Also make the Boolean true to keep it's global orientation similar. \$\endgroup\$ – Sourav Paul Aug 9 '16 at 4:15
  • \$\begingroup\$ I think it's a UI object. When I click the prefab in Unity, it says Layer: UI in the Inspector. I set it to true but same results. I did tag the Canvas (Custom Tag). \$\endgroup\$ – o.o Aug 9 '16 at 4:17
  • \$\begingroup\$ Sorry about the other error, was my mistake. However, I am getting this: Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption. UnityEngine.Transform:SetParent(Transform, Boolean) \$\endgroup\$ – o.o Aug 9 '16 at 4:28
  • \$\begingroup\$ Ooh, I fixed it. I had to use a temporary reference variable of type GameObject to set the parent. Basically: GameObject enemy = Instantiate(enemyPrefab, new Vector3(0, 0, 0), Quaternion.identity) as GameObject; then use enemy to set the parent. You can add it to your post if you want. Thanks for the help! \$\endgroup\$ – o.o Aug 9 '16 at 4:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.