Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by edd677 · Feb 13, 2015 at 12:15 PM · c#uislider

Updating UISlider with value from variable.

Hi all,

I am using a slider to display a players current stamina. Very basic issue I am having here -

Why does this code:

     if(currentStamina >= 0)
         {
             currentStamina += Time.deltaTime * 2f;
         }

cause the UISlider to refill faster than this code:

 if(currentStamina >= 0)
         {
             currentStamina += Time.deltaTime * staminaRegenRate;
         }
 

even when staminaRegenRate is declared as a public float with a value of 2f? Also, where should I be making the statement:

staminaBar.value = currentStamina;

Currently I have it at the bottom of Update().

Thanks!

Comment
Add comment · Show 4
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 Graham-Dunnett ♦♦ · Feb 13, 2015 at 12:16 PM 0
Share

What makes you say the refill is faster. Sounds wrong to me.

avatar image edd677 · Feb 13, 2015 at 05:41 PM 0
Share

Thanks Glurth - your comment led me to a solution - the issue was that I was changing the values in the class declarations where it was being defined, not in the inspector - which I learnt should be the case for public variables. The changes in $$anonymous$$onodevelop were not being fed back to the inspector, so each time I made a change in the regen rate variable in $$anonymous$$onodevelop, it was having no impact when the game was initiated - so it just used the values from the inspector, which never changed!

Thanks!

avatar image Glurth · Feb 13, 2015 at 10:35 PM 0
Share

great! so, should I convert that "comment" to an "answer", or did it only lead you to look at something else? (sounds like we are talking about the same thing, but not sure)

avatar image edd677 · Feb 13, 2015 at 10:57 PM 0
Share

By all means, convert it to answer! I would have done it myself but couldn't see an option to?

1 Reply

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

Answer by Glurth · Feb 13, 2015 at 03:21 PM

Make sure you assign the value to staminaRegenRate in start() or in the editor. If you assigned it as a default value in your class, that value will be overwritten by whatever value you have in the editor, at game launch.

Debug.Log() is your best friend when you are getting unexpecteed results like that. What does Debug.Log() say the actually value of staminaRegenRate is?

Update() should be a good place to update the slider value.

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 Kiwasi · Feb 13, 2015 at 11:15 PM 0
Share

I would make a slight suggestion to change the slider value within the if. That way you don't update the bar every frame if it is full.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I move my train with a slider, and not W & S 1 Answer

Saving a UI Text Number Multiple Times 2 Answers

Problem with UI slider to make it increment other variables 1 Answer

How to get float value based on the element size? 0 Answers

UI Slider, Save Values and Change Them 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