Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cjdev · Feb 03, 2016 at 01:55 AM 0
Share

What does your CBT$$anonymous$$anager's SetCBT method do?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I check if a function has been called recently? 1 Answer

How do I make the object apply damage on trigger? 0 Answers

How to do multiple inheritance (or workaround) with Unity? 1 Answer

Best practice where to store Projectile Damage amounts? 1 Answer

Do Something When a Object is hit by a raycast 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges