Enemy wont damage player

Hey, im kinda a beginner so i dont understand complex stuff.
im trying to make a game where the enemy will damage the player when it walks inside of my box collider
The enemy has a box collider set to trigger
This is my scripts:

Enemy Script:

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

public class EnemyAttack : MonoBehaviour {
    public bool inRange;
    public float damage;
	
	void Start ()
    {
		
	}
	
	
	void Update ()
    {
	  if(inRange == true)
        {
            //attack
            PlayerStats player = GetComponent<PlayerStats>();
             player.P_TakeDamage(damage);
            
               
            
        }
	}

    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            inRange = true;
        }
    }

     void OnTriggerExit(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            inRange = false;
        }
    }

}

Player Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerStats : MonoBehaviour {
    public float currHealth;
    public float maxHealth;
	
	void Start ()
    {
		
	}


    public void P_TakeDamage(float ammount)
    {
        currHealth -= ammount;
    }



    void Update ()
    {
		if (currHealth > maxHealth)
        {
            currHealth = maxHealth;
        }

        if (currHealth <= 0)
        {
            //Die
            Die();
        }

    }
    public void Die()
    {
        Destroy(gameObject);
    }



}

2 Answers

2

Did you try debugging the code and watch values of inRange and currHealth ?

@Santosh_Nair hmmm.. i dont really know what that means? as i said im a noob sorry, is it this? https://unity3d.com/learn/tutorials/topics/scripting/debugging-unity-games-visual-studio

if what i said earlier was right then it looks like this [126066-namnlos.png|126066]

Do they both have a rigidbody component attached? If not make sure to add one to each otherwise IIRC the OnTriggerEnter and OnTriggerExit will never be called. Otherwise click on the enemy in the editors hierarchy and check wither the “inRange” variable is set to true when the player gets near.

@ElementalVenom It says its true when i get close, but it wont damage the player. ive also tried to change player.P_TakeDamage(damage); to player.currHealth -= damage; but that doesnt work wither.