Animator and collision
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:
If anyone knows has any way to fix this please let me know, I can provide more pictures, video, ect.
Thank you!