gameObject.SetActive(false) crashes Unity

I am using Unity 4.3 for a 2D game

I have an animation on the scene playing and at the end I want it disabled. (The reason I wanted to disable it is to use pooling)

I created a trigger in the animation and attached a class with following function…

public void AnimationCompleted()
    	{
    		// this works 
    		Destroy(gameObject);
    
    		// this does not work, crashes unity
    		//gameObject.SetActive(false);
    	}

If I use,

gameObject.SetActive(false);

It crashes Unity editor.

If I destroy the object, it works perfectly.

Can anyone provide a solution for this?

I have attached the project to this post. Currently setActive line is commented in the project, so it does not crash.

[18973-setactivetest.zip|18973]

I tried your project and it happens to me too. It appears to be a bug. Unity apparently does not like it when the gameObject the animationEvent happens on gets disabled in the middle of the event or something similar.

A workaround could be to use a coroutine to wait for the end of the frame instead of disabling immediately:

public void AnimationCompleted()
{
    StartCoroutine(WaitDisable());
}

private IEnumerator WaitDisable()
{
    yield return new WaitForEndOfFrame();

    gameObject.SetActive(false);
}

Or only disable the SpriteRenderer to hide the Sprite:

public void AnimationCompleted()
{
    GetComponent<SpriteRenderer>().enabled = false;
}

If someone has this problem again, try something like this (when you really need to disable an object, not only the sprite):

private bool isAnimationCompleted;

public void AnimationCompleted()
{
    isAnimationCompleted = true;
}

void Update() 
{
    if(isAnimationCompleted)
    {
        isAnimationCompleted = false;
        gameObject.SetActive(false);
    }
}

It’s easier than the example above. Deactivation of the object will occur in the next frame.