Pause & Unpause Different timers

Hi Guys,

I have 2 timers, and I wish to pause them each at individually without affecting the other timer, and resume the time where it left.

Meaning, I would like to pause Timer A, while timer B continue to count down.
and when I click Resume for Timer A, it will continue the remaining time where it left previously.

The Problem (I):

I’m using the Time.timeScale. But it’s pausing the Time.time across the game scene.
Basically, I tried to come out with have the same function with different name like CountDown2 ()
And when I resume, Timer A’s Remaining time is not continue which it last left.

For instance, if I pause Timer A’s time, both timers, will be paused, if I unpause, both timers will resume with the different timing, it should be 12sec left, now it’s 10sec (because of the Time.time)

Problem (II)

I can’t pause each timer’s time individually.

Qns 1) Is there any way I could pause just one timer only and left the other continue to count down?
Qns 2) How do let the timer resume the time where it last left when I click the resume / unpause button?
Qns 3) What are other areas I can improve my coding (below)?

Thanks in advance!

Below is my coding:

    var startTime : float; 
    var timeRemain : float;// time remaining
    
    var clockIsPause2: boolean = false;
    var startTime2 : float; 
    var timeRemain2 : float;// time remaining for timer 2
    
    function Awake()
    {
    	startTime = 5;
    	startTime2 = 10;
    }
    
    function Update () {
    
    	
    	if (!clockIsPause)
    	{
    		//Debug.Log("clock is not pause");
    		CountDown ();
    	}
    	else
    	{
    		//Debug.Log("clock is PAUSEd);
    	}
    	
    if (!clockIsPause2)
    {
    		//Debug.Log("clock is not pause");
    		CountDown2 ();
    	}
    	else
    	{
    		//Debug.Log("clock is PAUSEd);
    	}
     }
    
    function OnGUI ()
    {
    	 
    	if (!clockIsPause)
    	{
    	   if ( GUI.Button(Rect(350, 350, 100,30), "PAUSE"))
           PauseClock ();
         } 
      
       if (clockIsPause)
        {  
         if( GUI.Button(Rect(400, 350, 100,30), "Resume"))
        UnpauseClock ();
          }	 
          
     
        if (!clockIsPause2)
    	{
    	   if ( GUI.Button(Rect(350, 420, 100,30), "PAUSE2"))
           PauseClock2 ();     
        } 
      
       if (clockIsPause2)
        {  
         if( GUI.Button(Rect(400, 420, 100,30), "Resume2"))
        UnpauseClock2 ();
        }	
    }
    
    function CountDown ()
    {
    	timeRemain = startTime - Time.time;
    	
    }
    function CountDown2 ()
    {
    	timeRemain2 = startTime2 - Time.time;
    }
    
    
    
    function PauseClock ()
    {
    	clockIsPause = true;
    	Time.timeScale = 0.0;
    	  Debug.Log("pause");
    }
    
    function UnpauseClock ()
    {
    	Time.timeScale = 1.0;
    	clockIsPause = false;
    	  Debug.Log("UNpause-ing");
    }
    
    function PauseClock2 ()
    {
    	clockIsPause2 = true;
    	Time.timeScale = 0.0;
    	Debug.Log("pause2");
    }
    
    function UnpauseClock2 ()
    {
    	Time.timeScale = 1.0;
    	clockIsPause2 = false;
    	Debug.Log("UNpause-ing2");
    }

For others searches, multi timers, multi-timer, multi-timers, different timers (since I can’t add new key words)

You have quite a bit of unnecessary stuff in there for one . . .

Try this multi-timer here!

class TimerClass {
   var FirstTimer : float = 0 ;
   var FirstTimerActive : boolean = true ;
   var SecondTimer : float = 0 ;
   var SecondTimerActive = true ;
}

var PlayerTimer : TimerClass ;


function Update ( )
{
   if ( PlayerTimer.FirstTimerActive ){
      PlayerTimer.FirstTimer -= 1 * Time.deltaTime ;
      if ( GUI.Button(Rect(350, 350, 100,30), "PAUSE")){
         PlayerTimer.FirstTimerActive = false ;
      }

   }else{
      if( GUI.Button(Rect(400, 350, 100,30), "Resume") ){
         PlayerTimer.FirstTimerActive = true ;
      }

   }

   if ( PlayerTimer.SecondTimerActive ){
      PlayerTimer.SecondTimer -= 1 * Time.deltaTime ;
      if ( GUI.Button(Rect(350, 350, 100,30), "PAUSE2")){
         PlayerTimer.SecondTimerActive = false ;
      }
   }else{
      if( GUI.Button(Rect(400, 420, 100,30), "Resume2") )
      {
         Player.SecondTimerActive = true ;
      }
   }
}

function OnGUI ( )
{
   if ( PlayerTimer.SecondTimerActive ){
      
      if ( GUI.Button(Rect(350, 350, 100,30), "PAUSE2")){
         PlayerTimer.SecondTimerActive = false ;
      }
   }

   if ( PlayerTimer.FirstTimerActive ){
      
      if ( GUI.Button(Rect(350, 350, 100,30), "PAUSE2")){
         PlayerTimer.FirstTimerActive = false ;
      }
   }
}

That is a rought draft, and I apologize if there are any syntax errors! With having that said, you should now have two working Timers :slight_smile: Good luck, and I hope this solves your issue .