- Home /
How to set On Screen Message w/ timer
I have a puzzle game where the player wanders this endless puzzle, and after a certain period of time, I want a message to pop up saying there is no end, and then the game ends. I am new to Unity and would like some assistance in setting this up, so any tutorials/scripts/hints would be very helpful, thanks!
Answer by Eric5h5 · Dec 06, 2010 at 10:17 PM
Just use Invoke:
var timeToWait = 120.0;
function Start () { Invoke("GameOver", timeToWait); }
function GameOver () { // do whatever here }
Answer by Jesus_Freak · Dec 06, 2010 at 09:32 PM
here's what i use to post time... you could use that to show a timer. right now, the time is always showing.... and you might want to change the hours, seconds, and minutes yourself, and maybe change the values in the script yourself. but i'll provide the message thing.
var showGUI = false; var Timer : int = 0; var seconds : int = 0; var CTRL : int = -100; var minutes : int = 0; var hours : int = 0; var CTimer : int = 0; function Update() { CTimer++; Timer--; if(Timer == CTRL) { seconds --; Timer = 0; } if(seconds == -60) { minutes --; seconds = 0; } if(minutes == -60) { hours --; minutes = 0; } if(hours == -25) { hours = 1; } if(CTimer == -500)//at about 5 seconds, do something. { //print GUI as in turn a showGUI boolean to true. showGUI = true; } }
//GUI function to print the time the player spent playing
function OnGUI()
{
if(showGUI)
{
GUILayout.Button("there is no end!");
}
GUI.Label(Rect(20,100, 400,30), "Time past: " +hours * -1+":"+minutes * -1+":"+seconds * -1);
}
I add the starting time at the bottom I am guessing, correct? Would it automatically count down or would I have to find another script to make it count down?
oops! sorry... that counts up.... i'll work on a timer countdown... sorry!
there. that sets the counter to count down and it might display the GUI as "Time past: 0:-2:-27" for example. 2 $$anonymous$$utes and 27 seconds passed. i don't think that's what you want, and i'm not sure if that's how it prints as the gui... i'll try it out. but that's basically good and updated as of now.
now, time counts up, but it's showing the time through the GUI as counting up, but the values themselves stay the same. and it counts "up" as regular.
by the way, the values are decreasing over time. believe it or not!
Answer by Statement · Dec 06, 2010 at 10:04 PM
I think this C# script should do what you're looking for. It waits 60 seconds, then pops up a big button over the screen. Once button is pressed, the application exits.
It only exits if it is a built executable.
using UnityEngine; using System.Collections;
public class ExitTimer : MonoBehaviour { public string message = "There is no end..."; private bool pressedOk = false; private bool showGui = false;
private IEnumerator Start ( )
{
// Wait one minute.
yield return new WaitForSeconds( 60.0f );
// Wait for button press.
yield return StartCoroutine( WaitForOkButton( ) );
// Quit application (doesn't work in Editor I think).
Application.Quit( );
}
private IEnumerator WaitForOkButton ( )
{
// Show gui
showGui = true;
// Wait for Gui
pressedOk = false;
while ( !pressedOk )
yield return null;
// Hide gui
showGui = false;
}
private void OnGUI ( )
{
if ( showGui )
{
Rect fullScreen = new Rect( 0, 0, Screen.width, Screen.height );
if ( GUI.Button( fullScreen, message ) )
{
pressedOk = true;
}
}
}
}
This example makes use of Co-routines. It allows your logic to pause its execution in place, without actually stalling the rest of the program. I suggest you learn about co-routines as they are really nice when working with scripts that require sequential action like the one you described (any waiting actions, basically).
To use this, just place the script on any game object (that isn't deleted, or the timer stops). You need to match the file name with the class name (unless they changed that since 2.6 era), so you should put this code in a new file called ExitTimer.cs.
See if you can't make the timer configurable by adding a new public field, so you can reuse your script a bit more. :)
Your answer
Follow this Question
Related Questions
Sending Messages to Other game Objects? 1 Answer
Triggered on screen hint message. 1 Answer
Parametrize generic event system (DRY it) 1 Answer
Sending Messages with Unet 2 Answers
End Game When Time Is Up 1 Answer