1

So my current code removes 5 from the curhealth variable in my enemys once the bullet collides with it. But the issue is, if there is no matter which enemy i shoot and hit, it removes from every single instantiated enemy. I only want to remove health from the enemy who gets hit, how can i fix this?

Enemy.cs

using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.AI;
using UnityStandardAssets.Characters.FirstPerson;


public interface IDamagable
{
    void TakeDamage(int damage);
}

public class Enemy : MonoBehaviour, IDamagable
{
    //Health
    public float maxHealth = 100f;
    static public float curHealth = 100f;

    //Health Bar
    public Image healthBar;
    public Transform goal;

    // AI Pathfinding Variables
    NavMeshAgent agent;

    // Enemy Collision Variables
    public float damageCooldown = 1f;

    // Start is called before the first frame update
    void Start()
    {
        curHealth = maxHealth;
    }

    // Update is called once per frame
    void Update()
    {
        // Pathfinding code(?)
        agent = GetComponent<NavMeshAgent>();
        agent.destination = goal.position;

        //Health Bar Image
        healthBar.fillAmount = curHealth / maxHealth;

        //Death Function
        if (curHealth < 1)
        {
            Destroy(gameObject);
            ZombieSpawner.zombieCount--;
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        curHealth -= Weapon.damage;
    }

    private void OnCollisionStay(Collision collision)
    {
        if (damageCooldown > 0f)
        {
            damageCooldown = damageCooldown - 0.1f;
        }

        if (collision.rigidbody.name == "Player" && damageCooldown <= 0f)
        {
            RigidbodyFirstPersonController.playerCurHealth = RigidbodyFirstPersonController.playerCurHealth - 10;
            damageCooldown = 1f;
        }
    }

    public void TakeDamage(int damage)
    {
        curHealth -= damage;
    }

    private void Awake()
    {
        //agent = GetComponent<NavMeshAgent>();
    }

}

Weapon.cs

using UnityEngine;
using UnityEngine.UI;

public class GunHit
{
    public float damage;
    public RaycastHit raycastHit;
}

public class Weapon : MonoBehaviour
{
    //Weapon Model
    public GameObject weaponModel;

    public LayerMask mask;

    //UI Variables
    public Text ammoUI;

    // Weapon Variables
    static public int damage = 5;
    public int ammo = 12;
    public int MaxAmmo = 12;
    public int reserveAmmo = 90;
    public AudioSource fire;
    public AudioSource empty;

    public bool isAiming = false;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        ammoUI.text = ammo + "/" + reserveAmmo;

        weaponFiring();

        weaponReload();
    }

    void weaponAiming()
    {
        if (Input.GetKeyDown(KeyCode.Mouse1))
        {
            //weaponModel.transform.position = 0, 0, 0;
        }
    }
    // Default Position = x0, y0, z0.5

    public GameObject projectilePrefab;
    public GameObject gunBarrel;
    

    void weaponFiring()
    {

        if (Input.GetKeyDown(KeyCode.Mouse0) && ammo > 0)
        {
            fire.Play();
            Instantiate(projectilePrefab, gunBarrel.transform.position, gunBarrel.transform.rotation);
            ammo--;
        }

        if (Input.GetKeyDown(KeyCode.Mouse0) && ammo == 0)
        {
            empty.Play();
        }
    }

    void weaponReload()
    {
        if (Input.GetKeyDown(KeyCode.R))
        {
            if (ammo == 0)
            {
                if (reserveAmmo >= MaxAmmo)
                {
                    ammo = MaxAmmo;
                    reserveAmmo = reserveAmmo - ammo;
                }
                else
                {
                    ammo = reserveAmmo;
                    reserveAmmo = 0;
                }
            }
            else if (ammo < MaxAmmo)
            {

                //Tiddies code
                reserveAmmo += ammo;
                ammo = 0;
                reserveAmmo -= ammo = (reserveAmmo < MaxAmmo ? (reserveAmmo) : (MaxAmmo));
                /*
                 * My code
                 * ammo = MaxAmmo - ammo;
                reserveAmmo = reserveAmmo - ammo;

                if (reserveAmmo > MaxAmmo)
                {
                    ammo = MaxAmmo;
                }
                else if (reserveAmmo < MaxAmmo)
                {
                    ammo = reserveAmmo;
                    reserveAmmo = 0;
                }*/
            }
        }
    }
}
Thatguy553
  • 57
  • 9
  • 2
    It's because your current health is a static field when it shouldn't be static – Ruzihm Jul 25 '20 at 22:54
  • If thats the case how should i make the curhealth accessible by other scripts n such? -edit: My bad, I actually forgot im not accessing the health by raycast anymore. This should work, thank you for pointing that out! – Thatguy553 Jul 25 '20 at 23:07
  • Does this answer your question? [Accessing a variable from another script C#](https://stackoverflow.com/questions/25930919/accessing-a-variable-from-another-script-c-sharp) – Ruzihm Jul 25 '20 at 23:09

1 Answers1

3

The issue is that your current health variable is static, that means that each enemy is using the same variable for health.

Static makes the varaible global to everybody.

remove the static modifier and the variable becomes personal to each enemy ;)

Watachiaieto
  • 417
  • 3
  • 10