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 NightmaresDev · May 09, 2017 at 08:32 AM · c#floatrpgstatshealth

Stats effecting the max Health Value

What I want to do here is change the MaxHealth Value each time the "Vitality Stat" Value changes. For example Maxhealth = 100 + (Vitality * 8); (This would give me an error message because I would do this.

 PlayerStats playerStats;
 
 public float MaxHealth = 100 + (playerStats.Vitality * 8);
 
 public void Start{
      ETC....
 }


This would give me a weird error message. Then I tried to do. public float maxHealth;

     void Start ()
     {
         maxHealth = (100 + (playerStats.Vitatlity * 8) / 2);
         currentHealth = maxHealth;
         currentMana = maxMana;
 
     }

This wouldnt Work either... And i also tried it in in the Update method which seemed more correct but still it didnt do what i wanted it to doo!

Here are my 2 scripts that are needed for this to hopefully work.

 public class PlayerHealth : MonoBehaviour {
 
     PlayerStats playerStats;
 
     public float currentHealth;
     public float maxHealth;
     public Slider playerHealthBar;
     public int healthRegen = 4;
 
     public float currentMana;
     public float maxMana = 1000;
     public Slider playerManaBar;
     public int manaRegen = 12;
 
 
     CharacterController playerMovement;
     private bool isDead;
 
 
     void Start ()
     {
         currentHealth = maxHealth;
         currentMana = maxMana;
 
     }
 
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Minus))
             DealDamage(6);
         if (Input.GetKeyDown(KeyCode.Period))
             UseMana(52);
 
         if (currentHealth < maxHealth)
             currentHealth += healthRegen * Time.deltaTime;
 
         if (currentMana < maxMana)
             currentMana += manaRegen * Time.deltaTime;
 
 
         if (currentHealth <= 0)
             KillPlayer();
     }
 
     public void DealDamage(int damageValue)
     {
         currentHealth -= damageValue;
 
     }
 
     public void SetMaxHealth()
     {
         currentHealth = maxHealth;
     }
 
     void UseMana(float manaUseValue)
     {
         currentMana -= manaUseValue;
 
         if (currentMana <= 0)
             currentMana = 0;
     }
 
 
     void KillPlayer()
     {
         Destroy(gameObject);
     }
 
 }

 public class PlayerStats : MonoBehaviour {
 
     public float Strength = 6;
     public float Vitatlity = 10;
     public float Agility = 6;
     public float Intellect = 6;
 
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 }
 

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

2 Replies

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

Answer by Patrick2607 · May 09, 2017 at 07:10 PM

You need to instantiate the PlayerStats before you can access its properties

 void Start ()
 {
     playerStats = new PlayerStats(); // <-- THIS
     maxHealth = (100 + (playerStats.Vitatlity * 8) / 2);
     currentHealth = maxHealth;
     currentMana = maxMana; 
 }

Also, if you are only using the PlayerStats to store the values (with some functions maybe), you don't need the MonoBehaviour implementation.

 public class PlayerStats
 {
     public float Strength = 6;
     public float Vitatlity = 10;
     public float Agility = 6;
     public float Intellect = 6;
 }

The PlayerStats class above will be enough. If you only need the properties, you can change the class to a struct.

Comment
Add comment · Show 7 · 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 NightmaresDev · May 09, 2017 at 07:31 PM 0
Share

But wouldn't it mean that if I had the maxHealth in the Start method. If the value of Vitality changes which it does after let's say a Level Up... wouldn't it mean that the health value would stay the same even if there is an increase of Vitality since it is set in the start method.

What's happening is that what you said works! but if I change the value of the Vitality the $$anonymous$$axHealth does not change with it.. how would I make it that the maxHealth would always Update when the Vitality Value changes!

avatar image Patrick2607 NightmaresDev · May 09, 2017 at 08:01 PM 1
Share

You can redeclare the maxHealth value. Let's say you want to LevelUp your character. Let's create a function for that:

 public class Player
 {
     ...
     public void LevelUp()
     {
         playerStats.Upgrade();
         Recalculate$$anonymous$$axHealth();
         Set$$anonymous$$axHealth(); //If you want to give the player full health after leveling up
     }

     private void Recalculate$$anonymous$$axHealth()
     {
         maxHealth = (100 + (playerStats.Vitality * 8) / 2);
     }    
     ...
 }

 public class PlayerStats
 {
     private float _upgradeRate = 1.1f;
 
     public float Strength = 6;
     public float Vitality = 10;
     public float Agility = 6;
     public float Intellect = 6;
 
     public void Upgrade()
     }
         Strength *= _upgradeRate;
         Vitality *= _upgradeRate;
         Agility *= _upgradeRate;
         Intellect *= _upgradeRate;
     }
 }

We can now call the Players' LevelUp method which upgrades(multiplies) all properties in the PlayerStats by 1.1 (you can adjust it how you want it, this is just an example). Then it recalculates the $$anonymous$$axHealth.

avatar image NightmaresDev Patrick2607 · May 09, 2017 at 08:37 PM 1
Share

Just wanna Thanks, alot! Your da Best!

$$anonymous$$aybe we will see each other in another of my questions ;) I am looking forward to it ;D!

THAN$$anonymous$$S!

Show more comments
avatar image
0

Answer by adamwolanski · May 10, 2017 at 01:07 PM

@NightmareTD There is no need to inherit MonoBehaviour in PlayerStats. In this case playerStats is null and you need to instantiate PlayerStats component. Alternatively you can try this:

 public class PlayerStats {
     public float Strength;
     public float Vitality;
     public float Agility;
     public float Intellect;
 
     public PlayerStats()
     {
      Strength = 6;
      Vitality = 10;
      Agility = 6;
      Intellect = 6;
     }
 }
 
 void Start ()
 {
     playerStats = new PlayerStats();
     ...
 }

To make sure please copy error message you received.

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

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

313 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Need help calling a variable from another C# script 1 Answer

How can i modify a float in another script in C#? 1 Answer

Float not resetting when I instantiate new player prefab 1 Answer

Getting XP when dies Help 1 Answer

More elegant way to constantly update player health algorithms. 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