countdown to start level?

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.

using UnityEngine;
using System.Collections;
using System.IO;
using System;
using System.Threading;

public class Timer : MonoBehaviour {

    static bool startGame=false;
   
    void Start () {
   
        startGame = true;
       
        if (startGame == true)
        {
            Thread.Sleep (1000);
            GameObject.Find("Text").guiText= "3";
            Thread.Sleep (1000);
            GameObject.Find("Text").guiText.text= "2";
            Thread.Sleep (1000);
            GameObject.Find("Text").guiText.text= "1";
            Thread.Sleep (1000);
            GameObject.Find("Text").guiText.text= "GO!";
            GameObject.Find ("Text").guiText.text = "";
        }
    }

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 )…

http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-text

Thank you for the video!! I fixed the countdown.

Would you also happen to know how to keep the player from moving while the countdown is happening?

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.

To stop the player from moving, I disable the motor script using GetComponent. and then enable it afterwards.

do you do this in the timer script or the player movement script?

It would be in the timer script: something like
GameObject.FindWithTag(“Player”).GetComponent().enabled = false;
at the start of the timer, and then true at the end of the timer.

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.

yes i changed the script and added a coroutine instead of using thread.sleep…that was one of the issues

public class Timer1 : MonoBehaviour {
using UnityEngine;
using System.Collections;
using System.IO;
using System;
using System.Threading;
using UnityEngine.UI;

public class Timer : MonoBehaviour {

    static bool startGame=false;
    public Text txt;

   
    // Use this for initialization

    void Awake()
    {
        txt = gameObject.GetComponent<Text>();
        }
    void Start () {
   
        startGame = true;
           
        if (startGame == true)
        {
        StartCoroutine("CountDown");
        }
    }

        IEnumerator CountDown()
{

        yield return new WaitForSeconds(1);
        txt.text= "3";
        yield return new WaitForSeconds(1);   
        txt.text= "2";
        yield return new WaitForSeconds(1);
        txt.text=  "1";
        yield return new WaitForSeconds(1);
        txt.text= "GO!";
        yield return new WaitForSeconds(1);
        txt.text= "";
//
    }
//    }

this stopped the timer from playing but not the character

It doesn’t have anything to do with the countdown, so it must have thrown an error or something.