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 wasicool7 · Aug 04, 2016 at 01:00 PM · playertimercountdownhealth

How to regenerate player's health after timer countdown is done.

HI, I have a script that regenerates player's health after 5 seconds:

   float counter;
 
     void  Update (){
           counter += Time.deltaTime;
            if(counter > 30minutes){
               curHealth++;
                 counter = 0.0f; }
               if(curHealth > 5)
                  curHealth = 5;
                  }

Which is attach to my player's health script. I want to start regenerating my player health back when timer is at 0. But I don't know how. This is my player health script:

 //Stats
 public int curHealth;
 public int maxHealth = 3;
 public Collider2D col;
 public PlayerHealth playerhealthRef;
 public TimeManager countDownTimer;
 float counter;
 public Animator anima; // drag the panel in here again
 private UI_ManagerScripts UIM;
 DateTime currentDate;
 DateTime oldDate;
 
 private GenerateEnemy generateEnemy = null;
 
 
 void Start ()
 {
     curHealth = maxHealth;
     currentDate = System.DateTime.Now;
     if (countDownTimer != null)
     {
         // countDownTimer.gameObject.SetActive(false);
         countDownTimer.Enable(false);
     }
     if (GameObject.Find("Spawner"))
     {
         generateEnemy = GameObject.Find("Spawner").GetComponent<GenerateEnemy>();
     }
 }
 
 
 void Update ()
 {
     counter += Time.deltaTime;
     if (curHealth > maxHealth) {
         curHealth = maxHealth;
     }
     if ((curHealth <= 0) && (!countDownTimer.gameObject.activeSelf)) {
         
         Die ();
     }
     if(counter > 5)
     {
         curHealth++;
         counter = 0.0f; 
     }
     if(curHealth > 3)
         curHealth = 3;
 }

 
 void Awake()
 {
     UIM = GameObject.Find ("UIManager").GetComponent<UI_ManagerScripts> ();
 }
 
 void Die() {
     UIM.EnableBoolAnimator(anima);
     PlayerPrefs.SetInt("RemainingLives", curHealth);
     PlayerPrefs.Save();
     
     if (generateEnemy != null)
     {
         generateEnemy.DestroyAllBlobs();
     }
     if (countDownTimer != null)
     {
         countDownTimer.Enable(true);
     }
 }    
 public void Damage(int dmg)
 {
     curHealth -= dmg;
 }
  }

And this is my Timer countdown script:

 public Text timer;
 int minutes = 1;
 int seconds = 0;
 float miliseconds = 0;
 
 [Range(1, 59)]
 public int defaultStartMinutes = 1;
 public bool allowTimerRestart = false;
 public bool useElapsedTime = true;
 
 private int savedSeconds = -1;
 private bool resetTimer = false;
 
 private DateTime centuryBegin = new DateTime(2001, 1, 1);
 
 private float tickPerSecond = 10000000.0f;
 
 public void Enable (bool enable)
 {
     gameObject.SetActive(enable);
     ResetTime(); // force the timer to restart
 }
 
 void Awake ()
 {
     minutes = defaultStartMinutes;
     if (PlayerPrefs.HasKey("TimeOnExit"))
     {
         miliseconds = PlayerPrefs.GetFloat("TimeOnExit");
         savedSeconds = (int)miliseconds;
         
         if (useElapsedTime && PlayerPrefs.HasKey("CurrentTime"))
         {
             int elapsedTicks = (int)(DateTime.Now.Ticks / tickPerSecond);
             int ct = PlayerPrefs.GetInt("CurrentTime", elapsedTicks);
             PlayerPrefs.DeleteKey("CurrentTime");
             elapsedTicks -= ct;
             if (elapsedTicks < miliseconds)
             {
                 miliseconds -= elapsedTicks;
             }
             else
             {
                 miliseconds = 0;
             }
         }
         
         minutes = (int)miliseconds / 60;
         miliseconds -= (minutes * 60);
         
         seconds = (int)miliseconds;
         miliseconds -= seconds;
         
         PlayerPrefs.DeleteKey("TimeOnExit");
         
     }
     savedSeconds = 0;
 }
 
 public void Update()
 {
     // count down in seconds
     miliseconds += Time.deltaTime;
     if (resetTimer)
     {
         ResetTime();
     }
     if (miliseconds >= 1.0f)
     {
         miliseconds -= 1.0f;
         if ((seconds > 0) || (minutes > 0))
         {
             seconds--;
             if (seconds < 0)
             {
                 seconds = 59;
                 minutes--;
             }
         }
         else
         {
             resetTimer = allowTimerRestart;
         }
     }
     
     if (seconds != savedSeconds)
     {
         //  Show current time
         timer.text = string.Format("{0}:{1:D2}", minutes, seconds);
         savedSeconds = seconds;
     }
 }
 
 void ResetTime()
 {
     minutes = defaultStartMinutes;
     seconds = 0;
     savedSeconds = 0;
     miliseconds = 1.0f - Time.deltaTime;
     resetTimer = false;
 }
 
 private void OnApplicationQuit()
 {
     int numSeconds = ((minutes * 60) + seconds);
     if (numSeconds > 0)
     {
         miliseconds += numSeconds;
         PlayerPrefs.SetFloat("TimeOnExit", miliseconds);
         
         if (useElapsedTime)
         {
             int elapsedTicks = (int)(DateTime.Now.Ticks / tickPerSecond);
             PlayerPrefs.SetInt("CurrentTime", elapsedTicks);
         }
     }
 }

}

Thank you :)

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

Answer by TBruce · Aug 04, 2016 at 11:34 PM

The easiest way would be to first add the following to your uses statements at the top of your timer script

 using UnityEngine.SceneManagement;

next modify the Update() function in the timer script to look like this

 public void Update()
 {
     // if we are still counting down
     if (((minutes * 60) + seconds) > 0)
     {
         // count down in seconds
         miliseconds += Time.deltaTime;
         if (resetTimer)
         {
             ResetTime();
         }
         if (miliseconds >= 1.0f)
         {
             miliseconds -= 1.0f;
             if ((seconds > 0) || (minutes > 0))
             {
                 seconds--;
                 if (seconds < 0)
                 {
                     seconds = 59;
                     minutes--;
                 }
             }
             else
             {
                 resetTimer = allowTimerRestart;
             }
         }
     
         if (seconds != savedSeconds)
         {
             //  Show current time
             timer.text = string.Format("{0}:{1:D2}", minutes, seconds);
             savedSeconds = seconds;
         }
     }
     else
     {
         // reload the current scene
         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
     }
 }

This essentially reloads the scene when the timer hits 0.

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 wasicool7 · Aug 04, 2016 at 11:47 PM 0
Share

Thank you, I will try this out :) I just tried it out and I'm getting this error:

error CS0234: The type or namespace name Scene$$anonymous$$anagement' does not exist in the namespace UnityEngine'. Are you missing an assembly reference?

avatar image TBruce wasicool7 · Aug 05, 2016 at 12:37 AM 0
Share

If I remember correctly Scene$$anonymous$$anagement was added in Unity 5.3.0. I am using Unity 5.3.2 and I know it works in this version.

avatar image wasicool7 TBruce · Aug 05, 2016 at 12:52 AM 0
Share

ah okay thank you, I have the 5.1.2. I'll update :) thank you

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

70 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

Related Questions

adding two integers together once every half second,adding two integers together with delay? 1 Answer

Countdown timer for starting game 0 Answers

I need a "loop" timer that disables a action until the timer resets. 0 Answers

Countdown timer in multiple scene? 1 Answer

slider for video player 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