Instantiating UI Prefab - Error: Setting parent of a transform which resides in a prefab is disabled. What's wrong with my code?
I've searched and read docs to no avail. I'm using Unity 4.6.2f1.
The Boss prefab is instantiated from another script; in its start method, it instantiates the BossHealth prefab (a UI Text element).
I know that the BossHealth prefab (being a UI element) should be instantiated as a child of a canvas and I attempt to make that happen in BossHealth's start method. I have a canvas in the scene named Canvas.
In the Inspector, I've set the public GameObject references (they're notated here in my code for clarity).
The script attached to the Boss prefab:
using UnityEngine;
using System.Collections;
public class Boss : MonoBehaviour {
public int bossHealth;
public GameObject bossHealthUI; // Set in the Inspector. Showing as "BossHealth"
void Start () {
bossHealth = 5;
Instantiate (bossHealthUI);
}
}
The script attached to the BossHealth prefab:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class BossHealth : MonoBehaviour {
public Transform canvas; // Set in the Inspector. Showing as "Canvas (Rect Transform)"
private Boss boss;
private Text text;
void Start () {
// This line is likely the cause of the error. Why?
transform.SetParent (canvas, false);
boss = GameObject.FindGameObjectWithTag ("Boss").GetComponent <Boss> ();
text = GetComponent <Text> ();
}
void Update () {
text.text = "Boss Health: " + boss.bossHealth;
}
}
Is the object put in the inspector for the variable "canvas" a prefab ?
@$$anonymous$$iraSensei - Yes, the canvas object set in the inspector is a prefab.
For what it's worth, the canvas itself was not modified in any way from its default, except that I added three UI prefab children to it (but not the BossHealth prefab, as I want that to be instantiated from a script).
So you have your answer :)
your script tries to put canvas as the parent of your current game object, but it is a prefab, and this is not allowed. It has to be a game object already existing in your scene (maybe an instantiation of your prefab canvas).
@$$anonymous$$iraSensei is right, no harm in using a prefab but you'd have to instantiate the prefab canvas and give that instance a unique name so you can use GameObject.Find to search for the instantiated canvas once it exists.
Or, as a canvas takes little resources on its own, just leave the canvas in the scene so you can still use your public statement.
You are not forced to find it after you create it :
void Start () {
GameObject canvasGO = (GameObject) Instantiate(canvas, transform.position, transform.rotation);
transform.SetParent (canvasGO, false);
...
}
Answer by Little_big · Sep 12, 2016 at 01:08 AM
GameObject convasGo = (Instantiate(panel, transform.position, transform.rotation, this.gameObject.transform) as GameObject);
Your answer

Follow this Question
Related Questions
Making a time bomb 1 Answer
Problems with instantiated objects in a circle formation 0 Answers
SetParent changing the relative position of the parent 1 Answer
Clone of clones 2 Answers
instantiated projectile conflicts 0 Answers