Question by 
               SmultronBusken · Feb 02, 2016 at 05:21 PM · 
                functiondamagecrashingfunction callhealth  
              
 
              Unity crashes ONLY when this function gets called many times per second. Help would be very appreciated.
I have a tower which attacks enemies who has a health script. When the tower attacks it calls the function DealDamage(int amount);. No problem, yet.
However, if the tower with low attack cooldown attacks and thus calling the DealDamage function many times a second, Unity randomly crashes after about 5-20 seconds of the tower attacking.
DealDamage function:
     //Call function to deal damage/decrease value of currentHealth.
     public void DealDamage(int amount){
         if (hasCombatTextOn)
             CBTManager.instance.SetCBT(amount.ToString(), this.gameObject.transform);
         if (this.gameObject.GetComponent<SpriteRenderer>()){
             SpriteRenderer a = GetComponent<SpriteRenderer>(); 
             if(this.GetComponent<OnHitEffect>()){                 //Checks if this is an unit
                 OnHitEffect onHit = this.GetComponent<OnHitEffect>();
                 onHit.hit = true;
             }
         }
 
         currentHealth -= amount;
         if (healthBar) {
             float h = (currentHealth / (float)maxHealth);
             SetHealthBar (h);
         }
     }
 
               Health script:
 using UnityEngine;
 using System.Collections.Generic;
 using UnityEngine.UI;
 
 public class Health : MonoBehaviour {
 
     //Public variables
     public int maxHealth;                        //Maximum health value
     public int currentHealth;                    //Current health value
     public bool hasHealthBar = true;            //Boolean for hpbar status
     public bool hasCombatTextOn = false;            //Boolean for hpbar status
 
     [HideInInspector]
     public bool isDead = false;                    //Boolean for death status
     [HideInInspector]
     public GameObject healthBar;
 
 
     // Use this for initialization
     void Start () {
         currentHealth = maxHealth;
         if (hasHealthBar) {
             GameObject go = this.transform.Find("HealthBar").gameObject;
             healthBar = go.transform.Find("Bar").gameObject;
             float h = (currentHealth/(float)maxHealth);
             SetHealthBar (h);
         }
     }
     
     // Update is called once per frame
     void Update () {
 
         // currentHealth can't proceed greater than maxHealth
         if (currentHealth >= maxHealth)
             currentHealth = maxHealth;
         // If currentHealth equals 0 or below aka dead, call function Dead();
         if (currentHealth <= 0)
             Dead();
     }
 
     //Call function to deal damage/decrease value of currentHealth.
     public void DealDamage(int amount){
         if (hasCombatTextOn)
             CBTManager.instance.SetCBT(amount.ToString(), this.gameObject.transform);
         if (this.gameObject.GetComponent<SpriteRenderer>()){
             SpriteRenderer a = GetComponent<SpriteRenderer>(); 
             if(this.GetComponent<OnHitEffect>()){                 //Checks if this is an unit
                 OnHitEffect onHit = this.GetComponent<OnHitEffect>();
                 onHit.hit = true;
             }
         }
 
         currentHealth -= amount;
         if (healthBar) {
             float h = (currentHealth / (float)maxHealth);
             SetHealthBar (h);
         }
     }
 
     //Call function to heal/increase value of currentHealth.
     public void DealHealth(int amount){
         if (hasCombatTextOn)
             CBTManager.instance.SetCBT(amount.ToString(), this.gameObject.transform);
         currentHealth += amount;
         if (healthBar) {
             float h = (currentHealth / (float)maxHealth);
             SetHealthBar (h);
 
         }
     }
     
     void Dead (){
         isDead = true;                             //Sets boolean isDead to true
         if(this.gameObject.CompareTag("Enemy")){
             GameManager.instance.waveScript.WaveIndex.Remove(this.gameObject);
         }
         Destroy (this.gameObject);                //Destroys object
     }
 
     void SetHealthBar(float hp){
         healthBar.transform.localScale = new Vector3(Mathf.Clamp(hp,0f ,1f), healthBar.transform.localScale.y, healthBar.transform.localScale.z);
     }
 
 }
 
 
              
               Comment
              
 
               
              Your answer