Search Unity

LevelCountdown before start 3...2...1 GO!

Discussion in 'Scripting' started by Uselchen, Jan 15, 2015.

  1. Uselchen

    Uselchen

    Joined:
    Jul 20, 2014
    Posts:
    11
    Hi there,

    im making a small Game for Gamedesign class where you have to navigate a sphere through a labyrinth kind of level as fast as you can. So obviously the scene has a timer in it to show you your personal best time for each level.

    My problem is i would like to have another timer right at the beginning of the scene
    (in the middle of the screen (maybe annimated numbers))
    which pause the game and counts from 3 to 0 and then unpause the game so that you can start your jurney through the lab.

    I guess the Countdown timer im looking for is similar to a Racing Game type of timer which are shown before the race starts.

    thanks in advance :)
     
  2. ldb

    ldb

    Joined:
    Apr 30, 2013
    Posts:
    40
    It's easy. Just use a coroutine...
    Code (CSharp):
    1.  
    2.     void Start()
    3.     {
    4.         StartCoroutine(Countdown(3));
    5.     }
    6.    
    7.     IEnumerator Countdown(int seconds)
    8.     {
    9.         int count = seconds;
    10.        
    11.         while (count > 0) {
    12.            
    13.             // display something...
    14.             yield return new WaitForSeconds(1);
    15.             count --;
    16.         }
    17.        
    18.         // count down is finished...
    19.         StartGame();
    20.     }
    21.  
    22.     void StartGame()
    23.     {
    24.         // do something...
    25.     }
    26.  
    http://docs.unity3d.com/Manual/Coroutines.html
     
    nelloe88 and moha_kun like this.
  3. Uselchen

    Uselchen

    Joined:
    Jul 20, 2014
    Posts:
    11
    thanks for you answer,
    but to be honest im a noob in Unity, i dont realy know how to use that script.
    I attached it to my player sprite and to my Main_Camera but neither works. It dont stopts the game when the scene is loaded.

    This is how i controll my Charakter if it helps

    Code (JavaScript):
    1.  #pragma strict
    2. public var moveSpeed = 2.0;
    3. function Update () {
    4.      if (Input.GetMouseButton(0)) {
    5.          var targetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    6.          targetPos.z = transform.position.z;
    7.          transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
    8.      }
    9. }
     
  4. moha_kun

    moha_kun

    Joined:
    Jul 17, 2017
    Posts:
    2
    i think it didnt work for you because its in C# and your character controller script is in javaScript;
     
  5. moha_kun

    moha_kun

    Joined:
    Jul 17, 2017
    Posts:
    2
    Thank you very much ,it worked perfectly;
     
  6. rahulpatil6220375

    rahulpatil6220375

    Joined:
    Dec 10, 2018
    Posts:
    19
    how to set a text 1 2 3 go
     
  7. saifshk17

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    488
    Declare a text in your script and say public Text CountDown; and then assign your int count to this string. For example;
    CountDown.text = count.ToString();

    Create a UI text in your Unity and drag and drop it to CountDown text.

    You will get more information through this link:
    https://unity3d.com/learn/tutorials/projects/roll-ball-tutorial/displaying-score-and-text

    Hope this helps. Cheers.
     
  8. rahulpatil6220375

    rahulpatil6220375

    Joined:
    Dec 10, 2018
    Posts:
    19
    text not change
     
  9. rahulpatil6220375

    rahulpatil6220375

    Joined:
    Dec 10, 2018
    Posts:
    19
    public Text countdowns;
    public int timerText;
    void Start()
    {
    StartCoroutine(Countdown(3));
    }
    void Update()
    {
    countdowns.text = timerText.ToString();

    }
    IEnumerator Countdown(int seconds)
    {
    int count = seconds;

    while (count > 0)
    {
    countdowns.text = timerText.ToString();
    yield return new WaitForSeconds(1);
    count--;
    Debug.LogError("count" + count);
    }
    StartGame();
    }

    timer should work text ui not change??