- Home /
Unity4: Change Hirachy OnDisable
Hello,
I just upgraded from Unity3.5 to Unity 4.2 and I get Problems with my Objectpools. The Objects in the Games are pooled for Memory and LoadingTime Optimisation.
Until now the Pool was working like this ( just the important parts for the Problem ) :
class ObjectWithPooledMembers{
void Start(){
pooledGameObject = Pool.Instance.Instatiate(pooledPrefab);
pooledGameObject.transform.parent = gameObject.parent;
}
void OnDisable()
{
Pool.Instance.Deactivate(pooledGameObject);
}
class Pool(){
public void Deactivate(GameObject object)
{
// this is no longer allowed in Unity4 - because its called from inside OnDisable
object.transform.parent = Pool.Instance.gameObject.transform;
object.SetActive(false);
}
}
But now I`m no longer allowed to do this change OnDisable ( Error: Cannot change GameObject hierarchy while activating or deactivating the parent. )
Is there any function that is called before the Parent is realy Disabled ? Or Any other Idea how to implment a Pool in Unity4 if you need to change the Hirachy on Enable / Disable ?
Thanks for your help!
Friedrich
Answer by POiD · Mar 18, 2015 at 08:15 PM
Came across this Question when searching for a solution my problem with Object Pools and returning items to the pool when I change scenes. I was having the exact same issue with OnDisable, but not for all my objects, which confused me.
Turns out the objects I had no issues with were controlled and tracked by the script, but when I got them from the pool I was setting their Parent object to be a child of the GameObject my script was on. When in OnDisable, they could successfully be returned to the Pool without issue.
Thus to solve my issue, I slightly changed how I was using the UI components I was using from my Pool and placed them on a Empty Child GameObject. This still allowed me to release them to the pool using OnDisable.
Before: Panel (with Script) -> PooledObject
After: Panel (with Script) -> Empty GameObject --> PooledObject
Give this a try if you have similar issues. Needs slight reworking of your scene, but it works.
Answer by Xtro · Jul 31, 2013 at 07:45 PM
As far as I know, you can't activate a child object if its parent is deactivated. But there is an internal state of being active even if the parent is deactivated.
Please see this link : http://docs.unity3d.com/Documentation/ScriptReference/GameObject-activeSelf.html
Thank you fpr the quick answer. But the Problem is not the activation or deactivation. The Problem is, that I can not change the hierachy of a tree, where there parent is going to disable. $$anonymous$$eaning: if the Parent is deleted - it first gets disabled ( and OnDisable is called ), and inside the OnDisable function I´m not allowed to unparent the children and put them back in the pool. So I need a function that is called in the process of deletion - but before OnDisable - or a total new idea for handling pooled objects.
I just upgraded my object pool code to U4, and faced similar problems. This change is plain stupid. Why does it need to prevent reparenting when disabled?
Can you please mark the correct answer if you fixed that problem?
Unfortunatly there is no "correct" answer. Basicly you cannot do this easy implementation of a pool any longer in U4. You have to re-write the whole system. So OnDisable gets never called - you have to detect yourself that the GO is going to be destroyed, and start your own pool logic.
Answer by angelonit · Oct 28, 2021 at 06:28 AM
What I ended up doing was saving the references to the gameobjects I wanted to change parents to in a list (from OnDisable() ) and parent them from the list afterwards.
Your answer
Follow this Question
Related Questions
Old Scene still visible after Loading new 0 Answers
Navmesh Unity Free 0 Answers
Augmented Reality and Location 1 Answer
Importing Animated Fbx into Unity 1 Answer