So I have a hierarchy pretty much like:
I disable the root. When this happens (using “OnDisable()”) I want to reparent C directly under A. If I have a script on A trying to do this, I get the error “Cannot change GameObject hierarchy while activating or deactivating the parent ‘B’”.
However, when I put the same script on the root, it works just fine…
I don’t get this; there is no change to whatever is being disabled (the root, and thus it’s whole hierarchy) nor did I change any parenting, and it all takes place in the same frame, doesn’t it? Can anyone explain me why this works when the script is on a node not involved in the reparenting steps (like root), but not when it is one that is (like A)
Could just be a quirk in the way Unity implements deactivating objects. Maybe they wrote it like this:
// Disallow hierarchy changes
// Deactivate all children (this will recursively call this same code on those children)
// Allow hierarchy changes
// Run OnDisable on this object's components.
That is how it seems to behave, yeah. Hoping someone from Unity Technologies will chip in on this one; I am a bit reluctant to go with this script-on-root method, since they must designed this ‘do not rearrange deactivating transforms’ for a reason
I actually have run into this same problem. On disable, I attempt to move a gameobject from one parent to another. Problem is when the scene ends, the parent is being disabled at the same time, throwing this error.