think of creating a checkerboard, each square is a prefab. then using user controls to rotate the entire board around as one piece. so all the prefabs get placed under an empty group. ( the whole thing is not one prefab because the there are 12 different types of squares, each level could contain any number or combination of the squares, one level could contain 16 copies of square A)
so…
//create empty nodes.
node1 = new GameObject (“node1”);
node2 = new GameObject (“node2”);
//add prefabs as children to nodes
var squareA : Transform;
Instantiate(squareA);
//control nodes with user input.
no problem here, no issue or warnings but it stops when i run it with the error:
“Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.
UnityEngine.Transform:set_parent(Transform)”
does this mean i can not create empty nodes at run time then group several prefabs under them?
am i going about this correctly?
any idea on other ways around this?
You have a comment saying “add prefabs as children to nodes”, but you don’t seem to show the lines of code where you actually set the prefabs’ parent variables. This code could be where the problem lies.
From the error, it looks like you’re not instantiating prefabs before you give them their parents. The error basically means that you can’t change the parent of a non-instantiated prefab.
So, Instantiate them, group them under your nodes as needed, and then set their parent to the node.
If node1 and node2 are prefabs, then you should instantiate copies of them first. Prefabs aren’t strictly present in the scene, so you can’t use them as parents for anything else.
Hi, I solved the same problem… First, you should make a Instantiate the Prefab, after that, you should find that instance and asign the parent… Something like this:
// This is the parent node
Transform parent = GameObject.Find("Suelo").transform;
// This is the prefab
GameObject prefab = (GameObject)Resources.Load("prefab");
// Add the instance in the hierarchy
Instantiate(objTiled);
// Find the instantiate prefab and asign the parent
GameObject.Find("NAME").transform.parent = parent;
I hope this solution help you… (Sorry by my English)
Remember that Instantiate returns the copied instance (prefab instance, copied gameobject etc). Many of you seem to be using GameObject.Find() to find the instance - this is wrong. If there was already an object with the same name, you might get that old instance instead of your new freshly created copy!
public class Spawner
{
public Transform m_prefab;
public void Start()
{
Transform t = Instantiate(m_prefab) as Transform; // instantiate prefab and get its transform
t.parent = transform; // group the instance under the spawner
t.localPosition = Vector3.zero; // make it at the exact position of the spawner
t.localRotation = Quaternion.identity; // same for rotation
t.gameObject.name = "My Awesome Instance!";
}
}