Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by larrielyn · Sep 10, 2011 at 11:08 AM · timetimerapplication.quit

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

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Sep 10, 2011 at 11:16 AM

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ztriv · Apr 11, 2012 at 01:44 AM 0
Share

I'm looking to achieve something similar to larrielyn, but I'm wanting to end the game once I've entered a trigger. Is the script you have used Javascript, because I'm not familiar with "#" commands in java, but I am very new to coding. Thanks in advance for your help.

It did work, but I would just like some explanation as to why if it's not too much trouble.

avatar image by0log1c · Apr 11, 2012 at 02:09 AM 0
Share

@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).

avatar image ztriv · Apr 12, 2012 at 01:23 AM 0
Share

oh, ok thanks for explaining that to me, becuase I didn't recognize those commands because I haven't learned this coding language yet, I've been trying to use Java. Which by the way is apparently very similar to javascript,lol. And I believe I understand the preprocessor tags a little better now.

avatar image Eric5h5 · Apr 12, 2012 at 01:34 AM 2
Share

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).

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Accumulating deltaTime oddity 1 Answer

How to make a timer read to the .001 of a second 2 Answers

Minute Timer Issue 1 Answer

Time based scoring 2 Answers

Accurate timing while using timeScale. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges