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
0
Question by edd677 · Feb 13, 2015 at 07:55 AM · c#uitimer

Updating stamina in UISlider

Hi all,

I'm fairly new to Unity (and coding in general), so please bare with me.

I've created a very basic environment for an RPG, to which I am adding functionality as a way of learning new concepts.

At present, I'm trying to add a UISlider to reflect the characters stamina. The stamina needs to fall as the player holds left shift, and regenerate when they release the key. I have set up the canvas, and positioned the slider. I have created a 'PlayerStats' script to hold the logic, and attached it to the player.

My problem is that the bar does not update when I use time.deltaTime - but does when I use time.time - which doesn't seem appropriate. Also, changing the float values of staminaFallRate and staminaIncreaseRate has no effect on the speed of the decrease/increase?

It would be really great if someone could please be so kind as to lend their thoughts :)

Since I am a true beginner, I ask you to also point out any other shortfalls/floors/inefficiencies in my syntax! Thank you!

 public class PlayerStats : MonoBehaviour {
 
     public int startingHealth = 100;
     public int currentHealth;
 
     public float startingStamina = 100f;
     public float currentStamina;
     public float staminaFallRate = 150f;
     public float staminaIncreaseRate = 2f;
 
     public int startingArcane = 100;
     public int currentArcane;
 
     public Slider healthBar;
     public Slider staminaBar;
     public Slider manaBar;
     public Image playerHurtImage;
     public AudioClip deathClip;
     public float flashSpeed = 5f;
     public Color flashColor = new Color (1f, 0f, 0f, 0.1f);
 
     AudioSource playerAudio;
     ThirdPersonCharacter thirdPersonCharacter; // Used to stop player from running around once they are dead.
     bool isDead;
     bool damaged;
     bool isRunning;
 
     void Awake()
     {
         thirdPersonCharacter = GetComponent<ThirdPersonCharacter>(); // Third person character is name of script we are getting.
         playerAudio = GetComponent<AudioSource>();
         currentHealth = startingHealth;
         currentStamina = startingStamina;
         staminaBar.value = currentStamina;
         currentArcane = startingArcane;
 
     }
 
     void Update()
     {
 
         if(Input.GetKey(KeyCode.LeftShift))
         {
             currentStamina -= Time.time / staminaFallRate;
             staminaBar.value = currentStamina;
         } 
         else if (currentStamina < startingStamina)
         {
             //staminaBar.value = Mathf.MoveTowards(currentStamina, startingStamina, 2f);
             currentStamina += Time.time / staminaIncreaseRate;
             staminaBar.value = currentStamina;
         }
 
         if (currentStamina >= startingStamina)
         {
             currentStamina = 100;
         }
     }
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
1

Answer by Qasem2014 · Feb 13, 2015 at 09:08 AM

Please Don't forget Import UI At Top Of Your Code :

 import UnityEngine.UI;

I Use This Code For Changing The Value Of Slider And Its Works Fine :

 var HealthSlider : GameObject; // The Slider
 var Life : int = 100; // Health Of Enemy
 
 function FixedUpdate()
 { HealthSlider.GetComponent(Slider).value = Life; }

Don't Forget : check The "Whole Numbers" Property In Slider If Your Variable Is Int (Like ME)

Hope Its Help :)

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 edd677 · Feb 13, 2015 at 11:36 AM 0
Share

Hi Qasem,

Thank you for your reply.

This does help, although I think it is Javascript? I am very new and am only using C# at this time. I did manage to get it working (I think!), so thank you anyway :)

avatar image Qasem2014 · Feb 13, 2015 at 11:54 AM 0
Share

hi

yes its java but its so easy to change to c# :

 using UnityEngine.UI;
 
 AND
 
 GameObject HealthSlider; // The Slider
 int Life = 100; // Health Of Enemy
 void FixedUpdate()
 { HealthSlider.GetComponent(Slider).value = Life; }

it should work =D

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Create a countdown in C# (inside Coroutine) 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Unity 5 - Time counter Up script (millisecond precision) UI 1 Answer

How to snap UI element anchor relative to image sprite? 2 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