magoo
magoo

Reputation: 161

unity3d GameObject is already being activated or deactivated

In Unity, at runtime, I am detaching a child object from its parent when it is disabled. An error is being logged to the console "GameObject is already being activated or deactivated".

Apparently this is because I am changing the parent in the same frame that the child is being deactivated. This happens even though I see in the hierarchy that the child is successfully detached. So, from my point, everything worked fine.

So, does anyone know if this is just a spurious error, is it something I need to actually care about?

Upvotes: 0

Views: 3593

Answers (1)

Pino De Francesco
Pino De Francesco

Reputation: 756

I believe you have something odd going on because such an error does not happen, and here how you can prove it.

Take this script:

using UnityEngine;

public class SyncTest : MonoBehaviour {

    private Transform cube;

    void Start () {
        cube = transform.GetChild(0);   // No tests as I know it's there
    }

    private void OnMouseUpAsButton()
    {
        cube.parent = null;
        gameObject.SetActive(false);
    }

}

Then create a sphere and put a cube as its child like in this:

The sphere and its child

Then attach the script to the sphere:

Script attached to the sphere

Run the scene and just click on the sphere, the result is that the sphere disappears and the cube stays on and is no longer a child, no errors at all are produced:

enter image description here

I believe you have some synchronization problem, running the two commands in some odd fashion, most likely you are calling the obj.SetActive(false); twice in a row.

Upvotes: 1

Related Questions