describe

When using an object pool in Unity, while the parent is hiding, return the child object to the pool, reset the parent object, and Unity will return an error:

Cannot set the parent of the GameObject ‘show_(Clone)’ while activating or deactivating the parent GameObject ‘db’.

UnityEngine.Transform:SetParent(Transform, Boolean)

why

As noted, Unity limits the ability to change the parent object on a child node when it is shown or hidden.

To solve

After an attempt, although the operation of the child node is limited, the child node of the child node is not limited, that is, an empty object is added in the middle to isolate, which can be put into the pool.

When needed, determine whether there is an intermediate object, if not, create it first:

    public bool ShowLive(string livePath)
    {
        if(! m_Transform) {// It can't be put back into the pool when disabled, so it can only go through the middle empty node
            var go = new GameObject();
            m_Transform = go.transform;
            m_Transform.SetParent(transform, false);
        }
        
        return true;
    }
Copy the code