Search Unity

WHY won't this instantiate as a child of my canvas?!

Discussion in 'Scripting' started by Roughrider, Jun 13, 2016.

  1. Roughrider

    Roughrider

    Joined:
    Jul 17, 2011
    Posts:
    73
    Edit: got it. A simple google search helps... Sorry folks...

    I'm tearing my hair out missing something simple I suppose... It pops up on play but in the root of the hierarchy. The console error is "Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption."

    Here we go. Thanks folks!
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class EnemyHealthController : MonoBehaviour
    4. {
    5.  
    6.     public GameObject canvas;
    7.     public GameObject enemyHealthBarPrefab;
    8.  
    9.     void Start()
    10.     {
    11.         Instantiate(enemyHealthBarPrefab);
    12.         enemyHealthBarPrefab.transform.SetParent(canvas.transform, true);
    13.     }
    14. }
     
    Last edited: Jun 13, 2016
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You're attempting to set the transform of the prefab, not the transform of the instantiated object.

    --Eric