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 )…
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.
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= "";
//
}
// }