x


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?

more ▼

asked Jul 27 '11 at 10:36 PM

Infamous911 gravatar image

Infamous911
0 7 8 9

(comments are locked)
10|3000 characters needed characters left

2 answers: sort newest

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();

}
more ▼

answered Jul 27 '11 at 11:11 PM

vxssmatty gravatar image

vxssmatty
106 4 6 6

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.

Jul 28 '11 at 12:24 AM Infamous911

You need to use an IEnumerator instead of a void then.

http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html

Set it to C#

Jul 28 '11 at 01:03 AM vxssmatty

Using this code: IEnumerator Awake() { yield return new WaitForSeconds(5.0F); }

I'm getting this error: Script error: Awake() can not be a coroutine.

Jul 28 '11 at 03:53 AM Infamous911

You can not use Awake() to yield.

You should this

void Awake()
{
    ToDoSome();
}

IEnumerator ToDoSome()
{
    yield return new WaitForSeconds(5.0F);
}
Jul 28 '11 at 04:50 AM YikYikHeiHei

This works, thanks!

Jul 28 '11 at 04:10 PM Infamous911
(comments are locked)
10|3000 characters needed characters left

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();
    }
}
more ▼

answered Jul 27 '11 at 11:38 PM

YikYikHeiHei gravatar image

YikYikHeiHei
311 8 9 13

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 becausevoid' is not an iterator interface type

Jul 28 '11 at 12:31 AM Infamous911

I have to change this script to C#, please check this.

And my script have not OnTriggerEnter(UnityEngine.Collider), which script?

Jul 28 '11 at 01:31 AM YikYikHeiHei
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x347
x261
x52
x18
x14

asked: Jul 27 '11 at 10:36 PM

Seen: 1480 times

Last Updated: Jul 28 '11 at 04:10 PM