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 /
This question was closed May 14, 2018 at 09:05 AM by Kodaigan for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Kodaigan · May 14, 2018 at 08:09 AM · variables

HP not being Set for Enemy class

Hey there, I'm a bit in a conundrum, did a few Debuggings tries and just can't find the issue. The following script is supposed to set the current health to max health on initialisation. But when I run the script and try to hit the enemy once, the "curHealth" is set to 0 at any given point of time, and thus get's killed with one hit. Does anybody have a clue what could be the issue/bug I have been overlooking the entire time?

 public class Enemy : MonoBehaviour {
     
         [System.Serializable]
         public class EnemyStats
         {
             public int maxHealth;
             private int _curHealth;
             public int curHealth
             {
                 get { return _curHealth; }
                 set { _curHealth = Mathf.Clamp(value, 0, maxHealth); }
             }
     
             public void Init()
             {
                 curHealth = maxHealth;
             }
         }
     
         public EnemyStats stats = new EnemyStats();
     
         public void DamageEnemy(int damage)
         {
             Debug.Log("I have " + stats.curHealth + " HP.");
             stats.curHealth -= damage;
             if (stats.curHealth <= 0)
             {
                 Debug.Log("Killing Enemy!");
                 //GameMaster.KillEnemy(this);
             }
         }
     }

Many thanks.

Best Regards Koda

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 Hellium · May 14, 2018 at 08:17 AM 0
Share

You seem to never call Init so the value of curHealthstays at 0. $$anonymous$$oreover, are you sure maxHealth is greater than 0?

avatar image Kodaigan Hellium · May 14, 2018 at 08:21 AM 0
Share

maxHealth is set inside unity as 100. Sorry, should have mentioned that. Where should I call the Init function then? Or should I turn the Init into Awake/Start function?

1 Reply

  • Sort: 
avatar image
0

Answer by Kodaigan · May 14, 2018 at 09:05 AM

After experimenting around,this script get's fixed if Init is replaced by Awake and the function taken out of the EnemyStats class function. Which corresponds to Hellium's comment that Initnever get's called. The script will look like this now:

     [System.Serializable]
     public class EnemyStats
     {
         public int maxHealth = 100;
         private int _curHealth;
         public int curHealth
         {
             get { return _curHealth; }
             set { _curHealth = Mathf.Clamp(value, 0, maxHealth); }
         }
     }
 
     public void Awake()
     {
         stats.curHealth = stats.maxHealth;
     }
 
 public EnemyStats stats = new EnemyStats();
 
     public void DamageEnemy(int damage)
     {
         Debug.Log("I have " + stats.curHealth + " HP.");
         stats.curHealth -= damage;
         if (stats.curHealth <= 0)
         {
             Debug.Log("Killing Enemy!");
             //GameMaster.KillEnemy(this);
         }
     }
Comment
Add comment · Show 2 · 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 Hellium · May 14, 2018 at 09:13 AM 0
Share

I would advise to keep the Init function and just call it in the Awake function of your $$anonymous$$onoBehaviour.

avatar image Kodaigan Hellium · May 14, 2018 at 03:13 PM 0
Share

I realized that in the end, that it didn't matter where the function was. I was a bit...inexperienced when I realized that I could've fixed it by calling it in either an Awake function outside of the EnemyStats class. or a Start function. Preferebly in Awake function. So yeah. I kinda re-refixed it. Thanks for the help though!

Follow this Question

Answers Answers and Comments

140 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

Related Questions

Change variable value while running. 1 Answer

[C#] Instantiating via class references (Need help understanding) 1 Answer

What do I do when This error shows up ? 1 Answer

cannot convert double to int when trying to increment another script's variable by 0.10 1 Answer

Accessing a text variable from another script 0 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