- Home /
Why is my goblin not taking damage
I am trying to get my goblin to take damage and I made a script for that however it isn't working. Can you please take a look at it? I will show the entire script but only the bottom part is relevent.
Edit: I forgot to add that when I pass the weapon through the health value doesn't change Edit 2: also the weapon has the weapon tag and both objects have the isTrigger enabled
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 public class Enemy
 {
     public float health;
     public float speed;
     public NavMeshAgent agent;
     public float inRange;
     
 
     public Enemy(float health, float speed, NavMeshAgent agent, float inRange)
     {
         this.health = health;
         this.speed = speed;
         this.agent = agent;
         this.inRange = inRange;
     }
 
 }
 
 public class EnemyBaseScript : MonoBehaviour
 {
     public float health;
     public float speed;
     public NavMeshAgent agent;
     public float inRange;
     public Enemy enemy;
     
     // Start is called before the first frame update
     void Start()
     {
         enemy = new Enemy(health, speed, agent, inRange);
         agent = gameObject.GetComponent<NavMeshAgent>();
     }
 
     public void Update()
     {
         Invoke("LookForPlayer", 5.0f);
     }
        
 
     public GameObject playerPos;
 
 
 
     public void LookForPlayer()
     {
         playerPos = GameObject.FindGameObjectWithTag("Player");
     }
 
     public bool FindPlayerPos(float inRange)
     {
         if ((playerPos.transform.position).sqrMagnitude >= inRange)
         {
             return true;
         }
 
         return false;
     }
 
     public void ChasePlayer(NavMeshAgent agent)
     {
 
     }
 
     private Weapon weaponGettingHitBy;
 
     private void OnTriggerEnter(Collider col)
     {
         if (col.gameObject.tag == "Weapon")
         {
             weaponGettingHitBy.item = col.gameObject;
             weaponGettingHitBy.damagedelt = col.gameObject.GetComponent<WeaponBaseScript>().damagedealing;
             TakeDamage(health, weaponGettingHitBy); //change back to enemy.health
         }
         
     }
 
     public float TakeDamage(float h, Weapon AttackingItem)
     {
         h = h - AttackingItem.damagedelt;
         return h;
     }
 }
 
 
This is the script for the weapon:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Weapon
 {
     public GameObject item;
     public float damagedelt;
 
     public Weapon(GameObject item, float damagedelt)
     {
         this.item = item;
         this.damagedelt = damagedelt;
     }
 }
 
 public class WeaponBaseScript : MonoBehaviour
 {
     public GameObject self;
     public float damagedealing;
     private Weapon weapon;
 
 
     // Start is called before the first frame update
     void Start()
     {
         weapon = new Weapon(self, damagedealing);
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 }
 
 
 
Answer by Eno-Khaon · Nov 19, 2021 at 07:35 PM
You're getting a new value for health, but doing nothing with it:
 TakeDamage(health, weaponGettingHitBy);
 // ...
 public float TakeDamage(float h, Weapon AttackingItem)
 {
     h = h - AttackingItem.damagedelt;
     return h;
 }
 You just need to apply that updated health value to the "Enemy" now:
 health = TakeDamage(health, weaponGettingHitBy);
Alternatively, you can use your "health" variable as-is in the TakeDamage() function instead of passing it in, or you could even pass health in by reference to keep the function the way it is:
 TakeDamage(ref health, weaponGettingHitBy);
 // ...
 public void TakeDamage(ref float h, Weapon AttackingItem)
 {
     h = h - AttackingItem.damagedelt;
 }
I also needed to add a rigidbody to the object after this but then it worked thanks.
Your answer
 
 
             Follow this Question
Related Questions
How to make enemys do damage and you dying etc 1 Answer
Damage dealt over time question, Need Help 1 Answer
Jumping not always work 2 Answers
Addjustcurrenthealth not working 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                