Search Unity

3, 2, 1 countdown on resume the game

Discussion in 'Scripting' started by Niki.j, Oct 14, 2013.

  1. Niki.j

    Niki.j

    Joined:
    Oct 17, 2012
    Posts:
    157
    Hello again all,

    I am trying to implement the countdown "3->2->1-> Go" on resume the game with the code below:

    Code (csharp):
    1.  
    2. IEnumerator getReady()    
    3. {
    4.     showCountdown = true;    
    5.  
    6.     countdown = "3";    
    7.     yield return new WaitForSeconds(0.5f);
    8.  
    9.     countdown = "2";    
    10.     yield return new WaitForSeconds (0.5f);
    11.  
    12.     countdown = "1";    
    13.     yield return new WaitForSeconds (0.5f);
    14.  
    15.     countdown = "GO";    
    16.     yield return new WaitForSeconds (0.5f);
    17.  
    18.     countdown = "";
    19.     showCountdown = false;
    20. }
    21.  
    22. void OnGUI()
    23. {
    24. if(!isResumed)
    25. {
    26.     if(GUI.Button(resumeRect,System.String.Empty,GUIStyle.none))//Resume Button
    27.     {
    28.         isResumed = true;
    29.        
    30.         StartCoroutine(getReady());
    31.         Time.timeScale = 1.0f;
    32.     }
    33. }
    34.  
    35.     if(showCountdown)
    36.     {    
    37.         resumeCountDownText.font.material.color = Color.red;
    38.         resumeCountDownText.text = countdown;
    39.     }
    40. }
    41.  
    but when i run this code it will also resume my game and player can able to play the game, and if i delete Time.timeScale = 1.0f from resume button, then my resumeCountDownText will show only "3" and the countdown will not get start so that i can resume the game after completing the countdown.

    please suggest where to call this StartCoroutine(getReady()) so that it will first do my countdown functionality and then resume the game for the player to play....?????:confused::confused::confused:

    Thanks in advance and sorry for the rent.
    Niki.j
     
  2. wandern3D

    wandern3D

    Joined:
    Oct 13, 2013
    Posts:
    64
    Try something like this:

    Code (csharp):
    1. function countDown(){
    2.     audio.pitch=.8;
    3.     audio.PlayOneShot(countSound);
    4.     levelText.material.color=Color.red;
    5.     levelText.text="GET READY";
    6.     yield WaitForSeconds(2);
    7.    
    8.     audio.pitch=1;
    9.     audio.PlayOneShot(countSound);
    10.     levelText.material.color=Color.yellow;
    11.     levelText.text="GET SET";
    12.     yield WaitForSeconds(2);
    13.    
    14.     levelText.material.color=Color.white;  
    15.     startGame();
    16. }
    In an OnGui function you can check if button pressed or so, and then function countdown

    In the Function on awake call Countdown

    Maybe this helps?
     
  3. Niki.j

    Niki.j

    Joined:
    Oct 17, 2012
    Posts:
    157
    View attachment $BreakoutGame.cs


    Hello,
    i have tried this too, but this does not make any change in the execution.
    It behaves like call the function/iEnumerator just for once, and when ever it execute yield it get stuck over there till the game get resumed(i.e Time.timeScale = 1.0f), if i resume the game and then call this function/IEnumerator, both countdown and game get start executing together, that should not happen.

    What should happen is when player resume the game a countdown get start and when countdown get finished then game should start automatically...
    please help if any one have any more ideas regarding this....:confused::confused::confused:

    you guys can also check my complete code file attached here.....

    Thanks in advance!
    Niki.j
     
    Last edited: Oct 15, 2013
  4. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    While the countdown is active, Time.timeScale should still be 0 (paused).

    In the Coroutine, you will need to keep track of Time.realtimeSinceStartup, checking against it every frame until it has passed a certain amount.

    Untested Example:
    Code (csharp):
    1.  
    2. IEnumerator getReady()    
    3. {
    4.     showCountdown = true;    
    5.  
    6.     countdown = "3";    
    7.     yield return WaitForRealSeconds(0.5f);
    8.  
    9.     countdown = "2";    
    10.     yield return WaitForRealSeconds (0.5f);
    11.  
    12.     countdown = "1";    
    13.     yield return WaitForRealSeconds (0.5f);
    14.  
    15.     countdown = "GO";    
    16.     yield return WaitForRealSeconds (0.5f);
    17.  
    18.     countdown = "";
    19.     showCountdown = false;
    20.  
    21.     Time.timeScale = 1;
    22. }
    23.  
    24. IEnumerator WaitForRealSeconds (float waitTime) {
    25.     float endTime = Time.realtimeSinceStartup+waitTime;
    26.  
    27.     while (Time.realtimeSinceStartup < endTime) {
    28.         yield return null;
    29.     }
    30. }
    31.  
    After the countdown is finished, reset Time.timeScale.
     
  5. Niki.j

    Niki.j

    Joined:
    Oct 17, 2012
    Posts:
    157
    Hello,


    thanks for the suggestion...
    i have tried this code too, but this also not working if time.timeScale = 0;
    any other suggestion or any tested code would be very much helpful...
    thanks
    niki.j
     
  6. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    I don't usually do this (it's usually more beneficial for you to learn how to fix it yourself), but here is the fixed code:
    Code (csharp):
    1.  
    2. IEnumerator getReady()    
    3. {
    4.     showCountdown = true;    
    5.  
    6.     countdown = "3";    
    7.     yield return StartCoroutine (WaitForRealSeconds(0.5f));
    8.  
    9.     countdown = "2";    
    10.     yield return StartCoroutine (WaitForRealSeconds(0.5f));
    11.  
    12.     countdown = "1";    
    13.     yield return StartCoroutine (WaitForRealSeconds(0.5f));
    14.  
    15.     countdown = "GO";    
    16.     yield return StartCoroutine (WaitForRealSeconds(0.5f));
    17.  
    18.     countdown = "";
    19.     showCountdown = false;
    20.  
    21.     Time.timeScale = 1;
    22. }
    23.  
    24. IEnumerator WaitForRealSeconds (float waitTime) {
    25.     float endTime = Time.realtimeSinceStartup+waitTime;
    26.  
    27.     while (Time.realtimeSinceStartup < endTime) {
    28.         yield return null;
    29.     }
    30. }
    31.  
     
    seanadeyemi likes this.
  7. Niki.j

    Niki.j

    Joined:
    Oct 17, 2012
    Posts:
    157
    Hello Daniel,

    Thank you so much for the help,it works perfectly.

    Thanks
    Niki.j