Hello Unity Answers,
I’m looking for some help on how to create a timer within my script which appears on screen when the player collides into a cube.
At the moment when the player collides into the cube a question appears on screen. I would like to display a timer counting down from 10 to 0, with 0 destroying the pop up box and the player going back to the start.
Any ideas/help/suggestions would be much appreciated.
using System;
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Question1 : MonoBehaviour {
private Rect windowRect = new Rect (500, 100, 400, 200); //Window size
public bool question1;
private int count;
public Text countText;
void start()
{
count = 0;
SetCountText ();
}
void OnGUI()
{
windowRect = GUI.Window (0, windowRect, WindowFunction, "Ebola Quiz Island"); //window on screen
}
void WindowFunction (int windowID)
{
// Draw any Controls inside the window here
GUI.Label (new Rect (30, 25, 200, 50), " What year did Ebola begin?"); // Question
if (GUI.Button (new Rect (20, 100, 100, 30), "1976")) // Correct Answer
{
Destroy (this.gameObject);
count = count + 1;
SetCountText ();
}
if (GUI.Button (new Rect (280, 100, 100, 30), "1986")) //Wrong answer
{
Destroy (this.gameObject);
Application.LoadLevel(Application.loadedLevel);
}
if (GUI.Button (new Rect (20, 150, 100, 30), "1996")) // wrong answer
{
Destroy (this.gameObject);
Application.LoadLevel(Application.loadedLevel);
}
if (GUI.Button (new Rect (280, 150, 100, 30), "1966")) // wrong answer
{
Destroy (this.gameObject);
Application.LoadLevel(Application.loadedLevel);
}
}
void SetCountText()
{
countText.text = "Score: " + count.ToString ();
}
}
Player collides with cube and question box appears below
I would like a timer to appear when this question box is on screen and counts down from 10 to 0, with 0 destroying the questionbox and making the player restart the game.