- Home /
 
How to use a timer with a button
 // timer object with timers srcipt
 
 public float timerJob01, timerJob02;
     
         public void SetTimers() 
         {
             timerJob01 = 3f;
             timerJob02 = 5f;
         }
 
 //player object wtih other stuff inside it
     public Timers timer;  // reference to timers script
     
     
       public void ExecuteJob01() 
         {
              if (timer.timerJob01<=3 && timer.timerJob01>0)
             {
                 timer.timerJob01 -= Time.deltaTime;
             }
         }
 
               I need help, i'm trying to fiugre out how to make happen that when i click the button a time starts when it reaches 0 it adds 1 to one variable. If i use the code inside the method Update(), it works fine if i use it ith button it doesn't work. I also tried to use loops but most of times unty crashes or it doesn't wok properly because execute the countdown really too fast.
Answer by Llama_w_2Ls · Jan 24, 2021 at 08:29 PM
You can use a coroutine to create a version of Update() for your button. For example:
 public void ExecuteJob01()
 {
       StartCoroutine(Timer());
 }
 
 IEnumerator Timer()
 {
      while (true)
      {
           yield return new WaitForEndOfFrame();
 
           // This is an Update method
           // that runs every frame
           // (without crashing)
      }
 }
 
                
              Ty man, i'll try out asap, first i need to understand what a coroutine is what it does excatly. I'm new to coding, started like 2 weeks ago from 0.
It works really ty, now i'll try to figure out how to stop it once it reaches 0 ^^
Answer by porchetto · Jan 25, 2021 at 09:19 AM
Man i have a problem, if i reset the timer and then re-execute the coroutine it acts weird, the timer is not calculated properly as the first time.
 public void ExecuteJob01()
     {
         StartCoroutine(Timer());
         timer.ResetTimers();
     }
 
     IEnumerator Timer()
     {
         while (true)
         {
             yield return new WaitForEndOfFrame();
             if (timer.timerJob01 <= 3 && timer.timerJob01 > 0)
             {
                 timer.timerJob01 -= Time.deltaTime;
             }
             if (timer.timerJob01 <= 0)
             {
                 StopCoroutine(Timer());
             }
 
              You have to reset the timers before you re-run the coroutine:
  public void ExecuteJob01()
      {
          // Resets timer BEFORE running coroutine again
          timer.ResetTimers();
 
          StartCoroutine(Timer());
      }
 
                   
                 I did it, but it still acts strange
  public void ExecuteJob01()
         {
             timer.ResetTimers();
             StartCoroutine(Timer());
             
         }
 IEnumerator Timer()
     {
         while (true)
         {
             yield return new WaitForEndOfFrame();
             if (timer.timerJob01 <= 3 && timer.timerJob01 > 0)
             {
                 timer.timerJob01 -= Time.deltaTime;
             }
             if (timer.timerJob01 <= 0)
             {
                 StopCoroutine(Timer());
             }
 
                   If i click the button to re execute the code the second calculation goes crazy, it happens only when i click the button (once the timer is finished), i tired to do the code without button on start() method and it works properly, but my goal is to do the stuff like managerial games where u click to do something wait for sec, it's done u click again and repeat the stuff.
I believe that the coroutine hasn't stopped running, or something. Could you try something like this:
 bool IsRunning = false;
 
 public void ExecuteJob01()
 {
      timer.ResetTimers();
      
      if (!IsRunning)
      {
            IsRunning = true;
           StartCoroutine(Timer());
       }
 }
 
 IEnumerator Timer()
      {
          while (IsRunning)
          {
              yield return new WaitForEndOfFrame();
              if (timer.timerJob01 <= 3 && timer.timerJob01 > 0)
              {
                  timer.timerJob01 -= Time.deltaTime;
              }
              if (timer.timerJob01 <= 0)
              {
                  IsRunning = false;
              }
           }
      }
 
                   Your answer
 
             Follow this Question
Related Questions
Changing TPP view to a FPP view after picking up object 1 Answer
How to reactivate items ? 4 Answers
How to activate gameObjects based on a boolean from another scene? 1 Answer
How do I make a crouch script unity 3d 1 Answer
Parenting Player to platforms, not parenting on the second platform? 2 Answers