If....Then...

Hi, I have a situation where, when my enemies get shot , I am trying to play an explosion animation…

I have set up every thing as the tutorials told me, but I have interference from my “gameObject.SetActive(false);” section … how do I correct it… Please help…

private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Player")
        {

            animator.SetBool("Die", true);
            gameObject.SetActive(false);
        }
    }

SO my explosion isn’t happening … but I need to set my Enemy as inactive so that the pooler can recycle him…
any suggestions?

So I’ve changed my coding to this:

private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Player")
        {
            animator.SetBool("Die", true);
        }
        else
        {
            gameObject.SetActive(false);
        }

    }

so this makes my explosion animation run but then it only sets the enemy as inactive and not the rest of the prefab…

Line 9 in that script will render the entire GameObject (in which it resides) inactive. This will, of course, affect all its children as well - but not its parent upwards.

What do you mean by this? Do you mean that the Enemy object is a child of another GameObject and it is that parent that you want to disable? Or something else?

That is the idea… but it is not happening…the Enemy is the parent, and the hoverboard is the child… after checking and checking, it seems that the code: SetActive… is still not running…


so at the moment when the Player collider touches the Enemy collider, the explosion animation runs, which mean that the Enemy disappears, but not the hover board, the the hover board and the last picture of the explosion animation, is still on the screen…

That statement is not strictly accurate. :slight_smile: An animation running does not, in and of itself, disable other objects.

I would recommend that you put a breakpoint on line 9 - the SetActive(false). Check in the debugger what objects are being deactivated. For example, this is the first time that you have mentioned there being a hoverboard. Maybe there are other objects / interactions going on as well. Step your code through in the debugger to see if, what you think is happening is what is actually happening … or not. :slight_smile:

Thanks… I will try …
The hoverboard is a child of the Enemy… this I created a prefab with… so I thought that the animation was going to “replace” the whole prefab when it ran… the animator is linked to the prefab… and the script is linked to the prefab…