- Home /
Setting parent of instantiated object fails (Error: setting parent of prefab is disabled...)
I have an object that I instantiate and I immediately attempt to set it as a child of something, which fails with the error "Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption". The pattern is something I've written many times before, and I understand what the error is saying, but I don't see how this is possible given the object has been instantiated and so shouldn't be a prefab. Here is the code:
var myRecipe = Instantiate(Resources.Load<recipe>("Recipes/woodenWall"));
myRecipe.transform.SetParent(transform);
The second line is the one that fails; unity takes me right there in the stack trace.
I can see the instantiated object in the hierarchy, and it is definitely the same object as the one stored in the variable "myRecipe", I just can't set it's parent (note: I can change to and get the same error). Has anyone ran into this problem before?
Thanks.
Answer by Miicck · Sep 02, 2017 at 02:57 PM
Ah, I found out what was going wrong: It wasn't the myRecipe.transform object that was having issues, it was the transform I was trying to set it as a child of. Apparently the error "Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption" in this case means "Setting the child of a transform which resides in a prefab is disabled to prevent data corruption".