Animator is not playing Animator Controller

Hello, I am very new to all of this and after following some tutorials I took on the task of creating player_health and enemy_damage scripts for my 2d plat former. I am trying to make so that every time the player collides with the enemy he takes 1 point of damage playing his hurt animation, and if he takes 10 point of damage he dies.

Now this is my Player_Health script: `

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player_Health : MonoBehaviour
{
public Animator animator;

public int maxHp;
int currentHp;

void Start()
{
    currentHp = maxHp;
}

public void PlayerDamage(int damage)
{
    currentHp -= damage;
    Debug.Log("player is hurt");
    animator.SetTrigger("Hurt");

    if (currentHp <= 0)
    {
        PlayerDie();
    }
}

void PlayerDie()
{
    Debug.Log("Player Died");

    animator.SetBool("IsDead", true);
   
    //GetComponent<PlayerMovement>().enabled = false;
    //GetComponent<Collider2D>().enabled = false;
    //this.enabled = false;

        
}

}`

And this is my Enemy_Damage script:

`using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy_Damage : MonoBehaviour
{
public LayerMask playerLayer;
public Transform attackPoint;

public GameObject player;

private bool isTouching;
public int attackDamage;

private void Start()
{
    isTouching = false;
}

void OnCollisionEnter2D(Collision2D col)
{
    if (col.gameObject.CompareTag("Player"))
    {
        isTouching = true;
        AttackPlayer();
    }
}

private void OnCollisionExit2D(Collision2D col)
{
    isTouching = false;
}

void AttackPlayer()
{
    
     player.GetComponent<Player_Health>().PlayerDamage(attackDamage);
    
}

}`

Now for some reason when the player collides the enemy the debug tells me he has been hit, but the CurrentHP does not go down, same with the debug for death.

And as soon as they make contact these 2 errors show up:
158380-error.png

If anyone knows has any way to fix this please let me know, I can provide more pictures, video, ect.

Thank you!

Well,
First try reformatting the code :slight_smile: it will help inspecting it

It looks like when the player and enemy in contact it makes damage continuously and too fast to notice it.

That’s i believe what makes the player dies instantly.

You can make a coroutine to execute time frame damage to the player.


Regards

@antonybocchino At the start of your script I see you define maxHp without a default value and you say currentHP = maxHp in the Start function. You should probably give maxHp a default value or make an Awake() function and set maxHp equal to something in there first. For example you would add something like this above the Start function:
void Awake(){maxHp = 10;}