Search Unity

Object Not Dying

Discussion in 'Scripting' started by TheBigChedder, May 17, 2019.

  1. TheBigChedder

    TheBigChedder

    Joined:
    Mar 24, 2019
    Posts:
    6
    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):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class WaveTrigger : MonoBehaviour
    6. {
    7.     public float health = 10f;
    8.     public bool dead = false;
    9.     public int countUp;
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.  
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         if (countUp >= 10)
    20.         {
    21.             dead = false;
    22.             gameObject.SetActive(true);
    23.             countUp = 0;
    24.         }
    25.     }
    26.     public void TakeDamage(float amount) //How enemy takes damage
    27.     {
    28.         health -= amount; //Health is subtracted by amount of damage taken
    29.         if (health <= 0f) //Activates when health is 0 or below
    30.         {
    31.             Die(); //Activates death script
    32.         }
    33.         void Die() //Activates death
    34.         {
    35.             GameObject gameManager = GameObject.Find("GameManager");
    36.             Spawner enemyWaves = gameManager.GetComponent<Spawner>();
    37.             Spawner level = gameManager.GetComponent<Spawner>();
    38.             Spawner enemyDrop = gameManager.GetComponent<Spawner>();
    39.             enemyWaves.enemyWaves = true;
    40.             level.level = 1;
    41.             enemyDrop.enemyDrop();
    42.             gameObject.SetActive(false);
    43.             dead = true;
    44.             StartCoroutine(countPlus());
    45.  
    46.         }
    47.     }
    48.     private IEnumerator countPlus()
    49.     {
    50.         while (dead == true)
    51.         {
    52.             yield return new WaitForSeconds(1f); //wait 1 second to count
    53.             countUp = +1;
    54.         }
    55.     }
    56. }
    The Gun's code that involves hurting objects
    Code (CSharp):
    1. void Shoot() //What happens upon shooting
    2.     {
    3.         muzzleFlash.Play(); //Gives muzzle flash effect
    4.         mAudioSrc.Play(); //Plays audio
    5.         RaycastHit hit; //Raycasts
    6.         AmmoElapsed -= 1f;
    7.         TimeUntilNextShot();
    8.         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range, layerMask)) //If gun hits object within range
    9.         {
    10.             Debug.Log(hit.transform.name);//Tells info in log on hit object
    11.  
    12.             KillableBox target = hit.transform.GetComponent<KillableBox>(); //Activates when anything with KillableBox script is hit
    13.             if (target != null) //If target isn't null
    14.             {
    15.                 target.TakeDamage(damage); //Gives enemy damage
    16.             }
    17.             Zombie enemy = hit.transform.GetComponent<Zombie>(); //Activates when anything with Zombie script is hit
    18.             if (enemy != null) //If target isn't null
    19.             {
    20.                 enemy.TakeDamage(damage); //Gives enemy damage
    21.             }
    22.             Enemy evil = hit.transform.GetComponent<Enemy>(); //Activates when anything with Zombie script is hit
    23.             if (evil != null) //If target isn't null
    24.             {
    25.                 evil.TakeDamage(damage); //Gives enemy damage
    26.             }
    27.             InfiniteTrigger inf = hit.transform.GetComponent<InfiniteTrigger>(); //Activates when anything with Zombie script is hit
    28.             if (inf != null) //If target isn't null
    29.             {
    30.                 inf.TakeDamage(damage); //Gives enemy damage
    31.             }
    32.             WaveTrigger wav = hit.transform.GetComponent<WaveTrigger>(); //Activates when anything with Zombie script is hit
    33.             if (wav != null) //If target isn't null
    34.             {
    35.                 wav.TakeDamage(damage); //Gives enemy damage
    36.             }
    37.         }
    38.     }
     
  2. Shack_Man

    Shack_Man

    Joined:
    Jun 7, 2017
    Posts:
    372
    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.
     
  3. TheBigChedder

    TheBigChedder

    Joined:
    Mar 24, 2019
    Posts:
    6
    Just tried this and nothing pops up
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Does this line from
    Shoot
    log out?