Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question Why is explosion animation not appearing when i shoot the enemy in unity 2D?

Discussion in 'Animation' started by EhabAnarchist, Oct 22, 2023.

  1. EhabAnarchist

    EhabAnarchist

    Joined:
    Jul 23, 2023
    Posts:
    5
    I set up a death animation clip in unity and added the sprites, it runs just fine in the preview player, but when I enter play mode to test it, nothing appears once the enemy is shot, i.e. the explosion animation does not appear, additionally there are no errors or messages in the console.

    I followed two youtuve videos so far, and neither have worked for me, the movement animation on the enemy works fine, that did not give me any trouble when i set it up at least.

    Here are the relevant scripts:

    using UnityEngine;

    public class EnemyDeath : MonoBehaviour { private Animator anim;

    void Start()
    {
    anim = GetComponent<Animator>();
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
    if (collision.gameObject.CompareTag("Arrow"))
    {
    Die();
    }
    }

    public void Die()
    {
    anim.SetTrigger("death");
    }

    }

    script 2:

    using UnityEngine;

    public class ShotEnemy : MonoBehaviour { private void OnCollisionEnter2D(Collision2D col) { if (col.gameObject.tag.Equals("Arrow")) { ScoreManager.instance.Addpoints(); // AudioManager.instance.PlaySFX("EnemyKill"); Destroy(col.gameObject); Destroy(gameObject); } } }

    Both scripts are attached to the sprite gameobject of the enemy i was testing it on, that's where the animator component is and it runs the movement animation just fine as I stated before. I posted the second one because maybe that's where the code should go?