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 /
avatar image
0
Question by RyanW99 · Apr 08, 2019 at 02:18 PM · gamesobject pool

Enemy Health Doesnt Increase or Reset

I had a working enemy system within my game before I tried learning and adding object pooling. The health would be set to 100 and increase per wave spawned and when the player died it would be set back to 100. Not sure if that was the best way to go about this. Anyhow, now my object pool is incorporated my health no longer works as intended. If I do health = 100; OnEnable() the health wont increase per wave. If I don't set it to 100, the health will increase but the old way of setting it to 100 on death doesn't work. Any help would be appreciated.

Comment
Add comment · Show 2
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 Tsaras · Apr 08, 2019 at 02:26 PM 0
Share

Post your code, it sounds like an easy problem to fix.

avatar image RyanW99 Tsaras · Apr 08, 2019 at 02:38 PM 0
Share

There's a lot of code so ill try and condense it to strictly just the code involving the health.

 public class EnemyBehaviour : $$anonymous$$onoBehaviour{
 public float currentHealth;

 private void OnEnable()
 {
     currentHealth = 100;
 }

 void OnTriggerEnter2D(Collider2D collision){
 if ( collision.tag == ("bullet")) TakeDamage(powerFactor);

 void TakeDamage(float powerFactor){
 currentHealth -= bulletDamage * powerFactor;
 if(currentHealth = 0) GameOver();
 }

 public class GameController : $$anonymous$$onoBehaviour {
 public float enemyHealth$$anonymous$$ultiplier;

 IEnumerator SpawnWaves()
 {
     yield return new WaitForSeconds(startWait);
     while (true)
     {
         for (int i = 0; i < hazardCount; i++)
         {
         GameObject enemy = ObjectPooler.SharedInstance.GetPooledObject("Enemy");
         enemy.GetComponent<EnemyBehaviour>().currentHealth *= enemyHealth$$anonymous$$ultiplier;
             
             if (enemy != null)
             {
                 enemy.transform.position = spawnPosition1;
                 enemy.transform.rotation = spawnRotation1;
                 enemy.SetActive(true);
             }
             yield return new WaitForSeconds(spawnWait);
         }
         yield return new WaitForSeconds(waveWait);
     }
 }
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by hameed-ullah-jan · Apr 08, 2019 at 02:42 PM

can you share your code, because your statement is not clear enough to get the whole info

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 RyanW99 · Apr 08, 2019 at 02:49 PM 0
Share

I updated my code to involve everything to do with my currentHealth as I have it. However, the extra is more to do with taking away health than making it increase

avatar image
0

Answer by Tsaras · Apr 08, 2019 at 05:38 PM

The problem is in this segment:

 for (int i = 0; i < hazardCount; i++)
          {
          GameObject enemy = ObjectPooler.SharedInstance.GetPooledObject("Enemy");
          enemy.GetComponent<EnemyBehaviour>().currentHealth *= enemyHealthMultiplier;
              
              if (enemy != null)
              {
                  enemy.transform.position = spawnPosition1;
                  enemy.transform.rotation = spawnRotation1;
                  enemy.SetActive(true);
              }
              yield return new WaitForSeconds(spawnWait);
          }

You are setting the enemy's health before you activate it. So OnEnable() is called after enemy.GetComponent<EnemyBehaviour>().currentHealth *= enemyHealthMultiplier; therefore resetting the health to 100.

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 RyanW99 · Apr 08, 2019 at 09:37 PM 0
Share

Thank you for the help! It now 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

103 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

Related Questions

Object pool with DOTS? 1 Answer

2 unity web games on one page.. 0 Answers

facebook integration with unity iphone games 1 Answer

Would it be a bad idea to develop 2D games like 2048 or tetris in Unity? 1 Answer

Can i sell games if i use Unity Free and i don't have a Company? 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