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 vittu1994 · Dec 22, 2016 at 12:45 PM · floatvaluemodify

Make a float update its value based on another floats value

To explain, i want a float that will change its value based on the value of another float. Lets say i have a float called Health that is 100f, and i reduce the floats value by 1 every second. By altering the Health float i want to also alter another float called Speed that is originally 1.1f. By reducing the Health float it automatically increases the Speed float by maybe 0.1f. So if the Health float goes from 100 to 99 in value, the Speed float goes up to 1.2f from 1.1f.

Hopefully i explained my issue, otherwise i can explain it more. This will be run during Update() so i need to create a modifier for the Speed float but i cannot add += since it adds every frame, i just want the float to update, so instead of += its just =.

Anyone know how i should think by creating this modifier, or is there a better solution which makes this issue more simple and not so complicated

Thanks

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
0
Best Answer

Answer by LK84 · Dec 22, 2016 at 02:35 PM

Just put it in the set accessor of Health:

 private  float _health;
 public float Health
 {
 get{return _health;}
 set
 {
 _health=value;
 Speed=//Whatever you want to enter here;
 }    
 }
Comment
Add comment · Show 3 · 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 vittu1994 · Dec 22, 2016 at 03:14 PM 0
Share

Dont i need to specify the amount of health?

 private float _health = 100f;

Also in the //Whatever you want to enter here.. Do i write how the Speed variable is being modified? Or the overall new value which Speed needs to be incremented by?

avatar image LK84 vittu1994 · Dec 22, 2016 at 06:19 PM 0
Share

well, you need to initialize it (i.e. in the Start() method):

     void Start()
     {
     _health=100;
     Speed=1.1f;
     }

Later in the code when you want to change the value of Health you simple write Health=newValue

For the Speed variable you just write whatever logic you want to implement. According to your opening post you want to increase the speed by 0.1 when the Health decreases by 1? Then you can do something like this:

      private  float _health;
      public float Health
      {
      get{return _health;}
      set
      {
     Speed+=(_health-value)/10; 
     _health=value;     
      }    
      }







avatar image vittu1994 LK84 · Dec 29, 2016 at 11:32 AM 0
Share

ok, and newValue is just any value which i can decide to alter the Health variable with? And as long as i have this code you wrote i can alter the speed variable manually by changing the Health variable? Im pretty unfamiliar with how get set works

EDIT: I managed to make it work now! Thanks a ton!

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

84 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

Related Questions

Show Float and edit from another script. 2 Answers

How to get difference between two values 2 Answers

PLEASE TELL ME WHAT IS WRONG HERE I RECIEVE THIS ERROR 1 Answer

transform.position inconsistent numbers problem 1 Answer

Analytics - Funnel parameter (float) is always 0 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