- Home /
Why does my game go straight to game over when game starts?
I have a trigger that starts a timer and i want it to load my game over screen when it gets to 0 but before i can play the game to activate the trigger it goes to the game over screen. this is my code:
var AlarmSetOff = false;
var Alarm : AudioClip;
var endTime : float;
var textMesh : GUIText;
function OnTriggerEnter (other : Collider) {
AlarmSetOff = true;
endTime = Time.time + 60;
textMesh = GameObject.Find ("Timer").GetComponent(GUIText);
textMesh.text = "60";
}
function OnGUI () {
if (AlarmSetOff){
audio.PlayOneShot(Alarm);
}
}
function Update()
{
var timeLeft : int = endTime - Time.time;
if (timeLeft < 0){ timeLeft = 0;
textMesh.text = timeLeft.ToString();
OpenLevel("GameOverScreen");}
}
function OpenLevel(level : String){
Application.LoadLevel(level);
}
@script RequireComponent(AudioSource)
Why does this happen, any ideas ??
You'll need to re-paste that code and use the code button (101-010 square) to format it so it's readable.
Answer by Bunny83 · Dec 18, 2012 at 06:52 PM
The answer is quite simple. You have a timeout variable which get set to the current time + 69 when you enter the trigger. This will effectively "wait" 60 sec. from the point where you enter the trigger, before the gameover screen is displayed.
However you forgot something important. What's the initial value of you timeout? Right, it's 0. That means when you start your game, endtime - Time.time will be immediately smaller than 0.
There are two solutions for this:
initialize the endTime variable with a really big number (you could also use "float.PositiveInfinity")
Use an additional boolean flag which indicates that you have already entered the trigger.
The second version is the most robust one. Just imagine what happens in the first case when you reenter the trigger several times. The timeout would restart. Using a flag you can pervent this. You actually already have a boolean (AlarmSetOff) which you can use:
var AlarmSetOff = false;
var Alarm : AudioClip;
var endTime : float;
var textMesh : GUIText;
function OnTriggerEnter (other : Collider)
{
if (!AlarmSetOff) // Not triggered yet? --> trigger it
{
AlarmSetOff = true;
endTime = Time.time + 60;
textMesh = GameObject.Find ("Timer").GetComponent(GUIText);
textMesh.text = "60";
}
}
function Update()
{
if (AlarmSetOff)
{
audio.PlayOneShot(Alarm);
var timeLeft : int = endTime - Time.time;
textMesh.text = timeLeft.ToString("00");
if (timeLeft < 0)
{
timeLeft = 0;
AlarmSetOff = false; // reset the condition
OpenLevel("GameOverScreen");
}
}
}
function OpenLevel(level : String)
{
Application.LoadLevel(level);
}
Answer by Loius · Dec 18, 2012 at 05:28 PM
It looks like you have no brackets on your if. If only applies to the very next command (essentially, using curly brackets makes a set of commands into one 'block'. there's more exact words for it but that's the gist). In Python and related languages, tabbing things over counts as blocks, but in C and Unityscript, whitespace is ignored.
You want:
if ( condition ) {
statement1;
OpenLevel(blah);
}
Your answer
Follow this Question
Related Questions
Making a loadlevel script that saves a lot of space 1 Answer
Destroy trigger after a certain time 2 Answers
End Game when hit's water 2 Answers
How to load a level after 30 seconds? 3 Answers
How to stop a Countdown Timer? 1 Answer