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 nroesner · Jan 18, 2018 at 02:07 PM · timersliderhealthbartimer countdownc# tutorial

How do I decrease a slider over time?

Hello guys and gals,
the general idea is to create a slider which operates like health bar which decreases constantly over time so that the game is timebound and over after 5ish minutes. So far I have tried to set a countdown for the slider and entered the value of the total time to run down to zero like this:

public class Countdown : MonoBehaviour {

 public float timeLeft = 32100.0f;
 public bool stop = true;

 private float minutes;
 private float seconds;


 public void startTimer(float from)
 {
     stop = false;
     timeLeft = from;
     Update();
 }

 void Update()
 {
     if (stop) return;
     timeLeft -= Time.deltaTime;

     minutes = Mathf.Floor(timeLeft / 60);
     seconds = timeLeft % 60;
     if (seconds > 59) seconds = 59;
     if (minutes < 0)
     {
         stop = true;
         minutes = 0;
         seconds = 0;
     }
     //        fraction = (timeLeft * 100) % 100;
 }

}
A very big thank you in advance

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 PersianKiller · Jan 18, 2018 at 03:11 PM

hey its just an easy example I hope it gives you the idea.

 public Slider HealthBar;
 public float health;
 public float MaxHealth;// 1000


 void Start(){
 health=MaxHealth;
 HealthBar= GetComponent<Slider> ();

 HealthBar.maxValue =MaxHealth;
 HealthBar.value = health;

 }

 void Update(){
 health-=0.1f;
 HealthBar.value = health;

 }
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 nroesner · Jan 18, 2018 at 05:05 PM 0
Share

Awesome thank you very much it works perfectly fine now. Just the dot after the 1 in line 16 was to much

avatar image PersianKiller nroesner · Jan 18, 2018 at 05:06 PM 0
Share

yes you're right.glad it worked :)

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

127 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

Related Questions

"Clipping", "Jumping" UI slider, attached to the 2d game object ( health bar) 0 Answers

How to make a depleting bar timer GUI please help! 0 Answers

Change the Intensity of a Haptic Feedback with respect to time ? 0 Answers

Problems showing accurate time with FixedUpdate() 0 Answers

Time Management. Morning, noon and night. 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