Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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?