I'm trying to make an object get killed upon shooting it. The object seems to be ignoring the fact I put 50 rounds in it. The Object's Code Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class WaveTrigger : MonoBehaviour { public float health = 10f; public bool dead = false; public int countUp; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { if (countUp >= 10) { dead = false; gameObject.SetActive(true); countUp = 0; } } public void TakeDamage(float amount) //How enemy takes damage { health -= amount; //Health is subtracted by amount of damage taken if (health <= 0f) //Activates when health is 0 or below { Die(); //Activates death script } void Die() //Activates death { GameObject gameManager = GameObject.Find("GameManager"); Spawner enemyWaves = gameManager.GetComponent<Spawner>(); Spawner level = gameManager.GetComponent<Spawner>(); Spawner enemyDrop = gameManager.GetComponent<Spawner>(); enemyWaves.enemyWaves = true; level.level = 1; enemyDrop.enemyDrop(); gameObject.SetActive(false); dead = true; StartCoroutine(countPlus()); } } private IEnumerator countPlus() { while (dead == true) { yield return new WaitForSeconds(1f); //wait 1 second to count countUp = +1; } } } The Gun's code that involves hurting objects Code (CSharp): void Shoot() //What happens upon shooting { muzzleFlash.Play(); //Gives muzzle flash effect mAudioSrc.Play(); //Plays audio RaycastHit hit; //Raycasts AmmoElapsed -= 1f; TimeUntilNextShot(); if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range, layerMask)) //If gun hits object within range { Debug.Log(hit.transform.name);//Tells info in log on hit object KillableBox target = hit.transform.GetComponent<KillableBox>(); //Activates when anything with KillableBox script is hit if (target != null) //If target isn't null { target.TakeDamage(damage); //Gives enemy damage } Zombie enemy = hit.transform.GetComponent<Zombie>(); //Activates when anything with Zombie script is hit if (enemy != null) //If target isn't null { enemy.TakeDamage(damage); //Gives enemy damage } Enemy evil = hit.transform.GetComponent<Enemy>(); //Activates when anything with Zombie script is hit if (evil != null) //If target isn't null { evil.TakeDamage(damage); //Gives enemy damage } InfiniteTrigger inf = hit.transform.GetComponent<InfiniteTrigger>(); //Activates when anything with Zombie script is hit if (inf != null) //If target isn't null { inf.TakeDamage(damage); //Gives enemy damage } WaveTrigger wav = hit.transform.GetComponent<WaveTrigger>(); //Activates when anything with Zombie script is hit if (wav != null) //If target isn't null { wav.TakeDamage(damage); //Gives enemy damage } } }
Not sure why this doesn't work. Try adding some print messages, like in TakeDamage add print("damage taken " + amount); to see if it gets triggered and with the right damage amount.