- Home /
Pause at end of game
So I'm almost done with my Space Invaders style game, but I need a way to pause the game when you win (or lose) for a short amount of time, so that the player has time to see the text of "you win" or "you lose" before the application closes through Application.Quit(); Is there any way to do this without setting up some sort of complex timer thing?
Answer by vxssmatty · Jul 27, 2011 at 11:11 PM
I assume you call Application.Quit() from a coroutine... so...
var endOfGamePause : int; //set wait time here
function QuitGame () {
yield new WaitForSeconds(endOfGamePause);
Application.Quit();
}
I'm doing this in C# not JS so my 'function' is void, and it's giving me an error about not being able to return anything in a void function.
You need to use an IEnumerator ins$$anonymous$$d of a void then.
http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html
Set it to C#
Using this code: IEnumerator Awake() { yield return new WaitForSeconds(5.0F); }
I'm getting this error: Script error: Awake() can not be a coroutine.
You can not use Awake() to yield.
You should this
void Awake()
{
ToDoSome();
}
IEnumerator ToDoSome()
{
yield return new WaitForSeconds(5.0F);
}
Answer by YikYikHeiHei · Jul 27, 2011 at 11:38 PM
It can wait and show "you win" or "you lose".
Update
Change to C#
using UnityEngine;
using System.Collections;
public class YourScriptName : MonoBehaviour
{
public int WaitASecondsAndQuit = 3;
public bool isGameOver = false; //<-- this is set are game over?
public bool isYouWin = false; //<-- this is set are the player win?
void OnGUI ()
{
if (isGameOver)
{
if (isYouWin)
GUI.Label(new Rect(Screen.width/2,Screen.height/2,200,50),"You win");
else
GUI.Label(new Rect(Screen.width/2,Screen.height/2,200,50),"You lose");
}
}
IEnumerator QuitGame ()
{
isGameOver = true; //<-- And set other script if(isWinLose){ } to pause the game object not do any thing
yield return new WaitForSeconds(WaitASecondsAndQuit);
Application.Quit();
}
}
But I'm doing this in C# and it is giving me the error: The body of SphereScript.OnTriggerEnter(UnityEngine.Collider)' cannot be an iterator block because
void' is not an iterator interface type
I have to change this script to C#, please check this.
And my script have not OnTriggerEnter(UnityEngine.Collider), which script?
Your answer
Follow this Question
Related Questions
Timer with 5-6 rounds that spawns enemies then end game 0 Answers
Pause Menu upon Idle (timer & button issues C#) 0 Answers
Pause Time.time? 1 Answer
Button on pause menu not showing up 0 Answers