GameObject is already being activated or deactivated

Hi all,

I´m having a little problem with my current development process…

When my player dies, the gameObject is deactivated.

So far so good.

In my game I have moving platforms with a parenting code like this, to make him move with the platform.

void OnCollisionExit2D(Collision2D other)
{

    if (other.transform.tag == "MovingPlatform")
    {
        transform.parent = null;
			onPlatform = false;
    

	
	}
}

}

The problem is:

If my player dies, while still being “part” of the moving platform, I receive the error in the title.

What could I do to prevent this?

Update:

Already tried some player == null stuff.

If I do so, the player is going for a wild imaginary ride on the moving platform from the position of the respawn… I guess he never realized, he left the moving platform that way.

Help pls. I don´t know what to do…

@misanthropowitsch I had a similar issue where even after I disabled my GameObject the trigger that it was in would still make a call the OnTriggerExit2D method even though I disabled the object. The OnTriggerExit2D would also try to disable the object and was clashing with me manually disabling it.

Try this: as soon as the player should die (and BEFORE you disable it) disable all Collider2D components on the object.

Collider2D[] colliders = GetComponentsInChildren<Collider2D>();
foreach (var collider in colliders){
	collider.enabled = false;
}

Then in your OnEnable method re-enable the colliders

Collider2D[] colliders = GetComponentsInChildren<Collider2D>();
foreach (var collider in colliders){
    collider.enabled = true;
}

NOTE: The GetComponentsInChildren will also get any Collider2D component that is on the main object. I’m just suggesting this because I don’t know how your object is set up.

For those who find this in the future, see this thread

Basically, use a public method to set parent before enabling / disabling

In this case, have a public method to set player’s parent to null before deactivatng player object