0
\$\begingroup\$

So I am trying to trigger this explosion animation after this enemy dies, but for some reason the animation doesn't trigger. I've made sure that the explosion animation is linked to "Any State", and that the transition from any state to the explosion animation has the trigger "Death". Here is my script (only the important parts):

using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour
{

    [System.Serializable]
    public class EnemyStats
    {
        public int maxHealth = 100;

        private int _curHealth;
        public int curHealth
        {
            get { return _curHealth; }
            set { _curHealth = Mathf.Clamp(value, 0, maxHealth); }
        }

        public void Init()
        {
            curHealth = maxHealth;
        }
        public int damage = 40;
    }

    public EnemyStats stats = new EnemyStats();

    private Animator anim;

    [Header("Optional: ")]
    [SerializeField]
    private StatusIndicator statusIndicator;

    void Awake()
    {
        anim = transform.GetComponent<Animator>();
    }


    public void DamageEnemy(int damage)
    {
        stats.curHealth -= damage;
        if (stats.curHealth <= 0)
        {
            GameMaster.KillEnemy(this);
            anim.SetTrigger("Death");
        }

        if (statusIndicator != null)
        {
            statusIndicator.SetHealth(stats.curHealth, stats.maxHealth);
        }
    }

    public void OnCollisionEnter2D(Collision2D _colInfo)
    {
        Player _player = _colInfo.collider.GetComponent<Player>();
        if (_player != null)
        {
            _player.DamagePlayer(stats.damage);
            DamageEnemy(999999999);
        }
    }
}

enter image description here

\$\endgroup\$
5
  • \$\begingroup\$ Will the script looks fine to me and I am not sure about the solution to the problem but I am just guessing that have you checked the "time scale" if the scale is 0 then animator won't work. also, make sure that the trigger is assigned to transition. \$\endgroup\$ – Swati Mar 17 '20 at 11:40
  • \$\begingroup\$ Sorry but where is time scale? I just got started in Unity 2 weeks ago, so I am still kinda noob. Also, the trigger is assigned to the transition, so I am not sure why it isn't working. \$\endgroup\$ – user136618 Mar 17 '20 at 12:06
  • \$\begingroup\$ Sorry for the delay... I hope you found a solution to your problem. But from time scale I mean "The scale at which time passes. This can be used for slow-motion effects or pausing games etc.". For more information, please visit the link. docs.unity3d.com/ScriptReference/Time-timeScale.html \$\endgroup\$ – Swati Mar 18 '20 at 5:25
  • \$\begingroup\$ pretty sure I didn't mess with the time scale. I didn't have the word: Time.timeScale in any of my scripts so... \$\endgroup\$ – user136618 Mar 18 '20 at 9:39
  • \$\begingroup\$ Humm... seems strange... Then try to debug inside "if (stats.curHealth <= 0) { Debug.Log("Dead") } and check if compiler reaches there or not if not then its means that condition is not getting true so there is a problem with damage and if you see debug message then its means that there is a problem with the animator so then I guess we have to investigate animator further. \$\endgroup\$ – Swati Mar 18 '20 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy