Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by ialinen22 · May 01 at 10:00 PM · prefabs

Prefab health always ends up negative

In my PlayerAttack class my attack method is supposed to damage the prefabs not instantly kill them. When I tried to debug the prefabs health shows up as negative when it supposed to start with 100.

{

bool canAttack = true; public int damage = 25; public float cooldown;

 int enemyHealth;

 GameObject enemyObj;
 public GameObject prefab;
 Health eHealth;
 int enemyH;

 void Start()
 {
     GameObject enemy = GameObject.FindWithTag("Enemy");

     if (enemy != null)
     {  
         enemyHealth = enemy.GetComponent<Health>().health;
     }
 }

 void Update()
 {
 
     if(Input.GetMouseButtonDown(0))
     {
         if(canAttack)
         {
             Attack();
         }
     }
 }

 void Attack()
 {
     enemyH = enemyH - damage;
     Debug.Log(enemyH);
     StartCoroutine(AttackCooldown(cooldown));
     

     if (enemyH <= 0)
     {
         Debug.Log("dead");
         Destroy(enemyObj);
     }

 }

  

 private void OnTriggerEnter(Collider other)
 {
      enemyObj = other.gameObject;

     if (enemyObj.tag == "Enemy")
     {
         Health eHealth = enemyObj.GetComponent<Health>();
         int enemyH = eHealth.GetComponent<Health>().health;
         enemyH = 100;
         Debug.Log("EnemyInRange");
     }
   
 }

 void OnTriggerExit(Collider other)
 {
     if (other.tag == "Enemy")
     {
         canAttack = false;
         Debug.Log("Outside of range");
     }
 }

 IEnumerator AttackCooldown(float cooldown)
 {
     canAttack = false;
     Debug.Log("cant attck");
     yield return new WaitForSeconds(cooldown);
     canAttack = true;
     Debug.Log("can attack");
     
 }

 

}

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Hellium · May 01 at 10:52 PM

I took the opportunity to clean your code.

CODE NOT TESTED

 public int damage = 25;
 public float cooldown;

 private Health enemyHealth;
 private bool inCooldown = false;

 private bool CanAttack => inCooldown == false && enemyHealth != null;

 void Update()
 {
     if(CanAttack && Input.GetMouseButtonDown(0))
     {
         Attack();
     }
 }

 void Attack()
 {
     // Implement the Damage method responsible for decreasing the health
     // & destroying the object if the health goes below 0
     // **IN THE HEALTH CLASS ITSELF**
     enemyHealth.Damage(damage);
     StartCoroutine(AttackCooldown(cooldown));
 }

 private void OnTriggerEnter(Collider other)
 {
     if (other.CompareTag("Enemy") && other.gameObject.TryGetComponent(out Health newEnemyHealth))
     {
         enemyHealth = newEnemyHealth;
         Debug.Log("Enemy In Range");
     }
 }

 void OnTriggerExit(Collider other)
 {
     if (other.CompareTag("Enemy") && other.GetComponent<Health>() == enemyHealth)
     {
         enemyHealth = null;
         Debug.Log("Outside of range");
     }
 }

 IEnumerator AttackCooldown(float cooldown)
 {
     inCooldown = true;
     Debug.Log("Attack cooldown started");
     yield return new WaitForSeconds(cooldown);
     inCooldown = false;
     Debug.Log("Attack cooldown elapsed");
 }
Comment
Add comment · Show 1 · Share
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 ialinen22 · May 01 at 11:19 PM 0
Share

Thank you! Works like a charm.

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

142 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can the animation editor create local rotational data? 3 Answers

Unity Water Help 2 Answers

Joining separated prefabs to one source prefab 0 Answers

Scaling down dynamically created objects 1 Answer

Prefab linking 3 Answers


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