did not QUIT when the Time is over ...?

my game did not QUIT when the time is over ? please help me …

private var startTime;

private var restSeconds : int;

private var roundedRestSeconds : int;

private var displaySeconds : int;

private var displayMinutes : int;

var customButton : GUIStyle;

public var countDownSeconds : int;

public var timed;

function Awake() {

startTime = Time.time;

}

function OnGUI () {

//make sure that your time is based on when this script was first called

//instead of when your game started

var guiTime = Time.time - startTime;

restSeconds = countDownSeconds - (guiTime);

//display messages or whatever here -->do stuff based on your timer

if (restSeconds == 60) {

    print ("One Minute Left");

}

if (restSeconds == 0) {

    print ("Time is Over");

    Application.Quit();

}

//GUILayout.EndArea();

    //do stuff here



//display the timer

roundedRestSeconds = Mathf.CeilToInt(restSeconds);

displaySeconds = roundedRestSeconds % 60;

displayMinutes = roundedRestSeconds / 60; 

timed =String.Format (“{0:00}:{1:00}”, displayMinutes, displaySeconds);

//print(timed);

text = String.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds); 

GUI.Label (Rect (300, 25, 100, 30), text, customButton);

}

1 Answer

1

Application.Quit doesn’t work in the editor. When you create a build everything should work as expected.

You can do it like this:

if (restSeconds <= 0) {

    #if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
    #endif
    Application.Quit();
}

This will stop the playmode in the editor. The pre-processor tags: “#if UNITY_EDITOR” and “#endif” are important otherwise you can’t create a build anymore ;). The #if block excludes the containing code when you create a build so it’s only included inside the editor.

@ztriv I'll quickly correct before someone else catches you. What you use is not Java - which is a different language - but an implementation of what could be called JavaScript. As for the # commands, they're called preprocessor directives, they're language independant and they're used to give indication to the compiler. In this case, the compiler will completely ignore the line between the #if#endif if the application is not in the Editor (just like the lines didn't exist).

Java is not similar to Javascript, it's a quite different language. Unity doesn't use Java (or Javascript for that matter--it's a custom language, often called Unityscript, but frequently called Javascript by Unity Technologies, which doesn't do anyone any favors).