Search Unity

Time Not Unpausing

Discussion in 'Scripting' started by DestinyAwaits, Nov 28, 2015.

  1. DestinyAwaits

    DestinyAwaits

    Joined:
    Jul 14, 2014
    Posts:
    3
    I have it set where in the gameDictionaries script it is calling stoptimer from timeScript in the makeProduct method. Then in the stopProduct method, I have it where it calls starttimer from the timeScript.

    gameDictionaries:

    Code (CSharp):
    1.  
    2. public void makeProduct(string message)
    3.  
    4.     {
    5.  
    6.         makeButton.gameObject.SetActive (true);
    7.  
    8.         makePanel.SetActive (true);
    9.  
    10.         make2button.gameObject.SetActive(true);
    11.  
    12.         totalDev.gameObject.SetActive(true);
    13.  
    14.         totalDev.GetComponent<Text>().text = "Total Development Cost: $" + totalMake.ToString();
    15.  
    16.         GetComponent<timeScript>().StopTimer()
    17.  
    18.     }
    19.  
    Code (CSharp):
    1.  
    2. public void stopProduct()
    3.  
    4.     {
    5.  
    6.         print (gameName);
    7.  
    8.         if (pickedsystem == "Develop for:")
    9.  
    10.         {
    11.  
    12.  
    13.         }
    14.  
    15.         else
    16.         {
    17.             makeButton.gameObject.SetActive(false);
    18.  
    19.             makePanel.SetActive(false);
    20.  
    21.         }
    22.  
    23.         GetComponent<timeScript>().StartTimer();
    24.  
    25.     }
    26.  
    27. }
    28.  
    timeScript:


    Code (CSharp):
    1. public void StartTimer()
    2.     {
    3.  
    4.         InvokeRepeating("TimerFunction", 0, 1);
    5.  
    6.     }
    7.  
    8.     public void StopTimer()
    9.     {
    10.  
    11.         CancelInvoke("TimerFunction");
    12.  
    13.         Time.timeScale = 0f;
    14.  
    15.     }
    16.  
    Code (CSharp):
    1. void Start()
    2.     {
    3.         InvokeRepeating("Timer", 0, 1);
    4.     }
    5.  
    How do I get time to unfreeze?
     
  2. DeathQuake

    DeathQuake

    Joined:
    Oct 24, 2012
    Posts:
    64
    I don't think invoke will work if timescale is 0. It cannot calculate the delay if time is stopped
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    Use a Coroutine, wait the amount of time using unscaled time:

    Code (csharp):
    1.  
    2. IEnumerator WaitForRealSeconds(float dur)
    3. {
    4.     float start = Time.unscaledTime;
    5.     while(Time.unscaledTime - start < dur)
    6.         yield return null;
    7. }
    8.  
     
  4. DestinyAwaits

    DestinyAwaits

    Joined:
    Jul 14, 2014
    Posts:
    3

    Is this for time freezing or unfreezing?

    Code (CSharp):
    1.  StartCoroutine(WaitForRealSeconds(time));
    Is this a valid way of using this Coroutine?