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()
{
}
}