Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

countdown to start level?

Discussion in 'Scripting' started by thehappyunicorn, Jan 7, 2015.

  1. thehappyunicorn

    thehappyunicorn

    Joined:
    Jan 3, 2015
    Posts:
    8
    Hello,
    I need to create a timer that will go at the beginning of the level, that countdowns from 3 so the player knows when to start the game (eg. 3..2..1..GO!). During this time, the player should not be able to move.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO;
    4. using System;
    5. using System.Threading;
    6.  
    7. public class Timer : MonoBehaviour {
    8.  
    9.     static bool startGame=false;
    10.    
    11.     void Start () {
    12.    
    13.         startGame = true;
    14.        
    15.         if (startGame == true)
    16.         {
    17.             Thread.Sleep (1000);
    18.             GameObject.Find("Text").guiText= "3";
    19.             Thread.Sleep (1000);
    20.             GameObject.Find("Text").guiText.text= "2";
    21.             Thread.Sleep (1000);
    22.             GameObject.Find("Text").guiText.text= "1";
    23.             Thread.Sleep (1000);
    24.             GameObject.Find("Text").guiText.text= "GO!";
    25.             GameObject.Find ("Text").guiText.text = "";
    26.         }
    27.     }
    28.  
    The error that shows up is :
    MissingComponentException: There is no 'GUIText' attached to the "Text" game object, but a script is trying to access it.

    However, i used UI text instead, because i dont have the option of adding GUI text for some reason(i dont have gameObject>create other )...
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
  3. thehappyunicorn

    thehappyunicorn

    Joined:
    Jan 3, 2015
    Posts:
    8
  4. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    230
    Put this at the top of the player Update method:

    if(Timer.startGame == false) return;

    This will immediately end the Update method right there as long as startgame is false, so none of the player code below it will run until it's true.
     
  5. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    To stop the player from moving, I disable the motor script using GetComponent. and then enable it afterwards.
     
  6. thehappyunicorn

    thehappyunicorn

    Joined:
    Jan 3, 2015
    Posts:
    8
    do you do this in the timer script or the player movement script?
     
  7. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    It would be in the timer script: something like
    GameObject.FindWithTag("Player").GetComponent<CharacterMotor>().enabled = false;
    at the start of the timer, and then true at the end of the timer.
     
    Last edited: Jan 8, 2015
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Does the script actually work? As I read it should block the main thread and freeze Unity for four seconds. All without displaying your countdown.
     
  9. thehappyunicorn

    thehappyunicorn

    Joined:
    Jan 3, 2015
    Posts:
    8
    yes i changed the script and added a coroutine instead of using thread.sleep...that was one of the issues
    Code (CSharp):
    1. public class Timer1 : MonoBehaviour {
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.IO;
    5. using System;
    6. using System.Threading;
    7. using UnityEngine.UI;
    8.  
    9. public class Timer : MonoBehaviour {
    10.  
    11.     static bool startGame=false;
    12.     public Text txt;
    13.  
    14.    
    15.     // Use this for initialization
    16.  
    17.     void Awake()
    18.     {
    19.         txt = gameObject.GetComponent<Text>();
    20.         }
    21.     void Start () {
    22.    
    23.         startGame = true;
    24.            
    25.         if (startGame == true)
    26.         {
    27.         StartCoroutine("CountDown");
    28.         }
    29.     }
    30.  
    31.         IEnumerator CountDown()
    32. {
    33.  
    34.         yield return new WaitForSeconds(1);
    35.         txt.text= "3";
    36.         yield return new WaitForSeconds(1);  
    37.         txt.text= "2";
    38.         yield return new WaitForSeconds(1);
    39.         txt.text=  "1";
    40.         yield return new WaitForSeconds(1);
    41.         txt.text= "GO!";
    42.         yield return new WaitForSeconds(1);
    43.         txt.text= "";
    44. //
    45.     }
    46. //    }
     
    Last edited: Jan 8, 2015
  10. thehappyunicorn

    thehappyunicorn

    Joined:
    Jan 3, 2015
    Posts:
    8
    this stopped the timer from playing but not the character
     
  11. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    It doesn't have anything to do with the countdown, so it must have thrown an error or something.