• 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
-1
Question by yce · Jan 19, 2014 at 06:59 PM · gameobjecttimetimescaledeltatime

Game over scene when the time is up ?

How can i do game over scene when the time is up in this code ?

using UnityEngine; using System.Collections;

public class Timer : MonoBehaviour { public float Seconds = 59; public float Minutes = 0; void Update () { if (Seconds <= 0) { Seconds = 59; if (Minutes >= 1) { Minutes--; } else { Minutes = 0; Seconds = 0; GameObject.Find ("TimerText").guiText.text = Minutes.ToString ("f0") + ":0" + Seconds.ToString ("f0"); } } else { Seconds -= Time.deltaTime; } if(Mathf.Round(Seconds) <= 9) { GameObject.Find ("TimerText").guiText.text = Minutes.ToString ("f0") + ":0" + Seconds.ToString ("f0"); } else { GameObject.Find ("TimerText").guiText.text = Minutes.ToString ("f0") + ":" + Seconds.ToString ("f0"); } } }

Comment
Add comment · Show 1
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 Graham-Dunnett ♦♦ · Jan 19, 2014 at 07:00 PM 1
Share

It's worth watching the tutorial video to learn how to format your code.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DRProductions · Jan 19, 2014 at 10:02 PM

First off make sure you format your code second make a script called Countdown or something and use this code JavaScript:

 var Minutes = 1; 
 function Start(){
 yield WaitForSeconds(Minutes * 60);
 Application.LoadLevel("NAMEOFSCENE");
 }

Or if you prefer C# (This may not work I don't use C# but you get the idea):

 public class Example : MonoBehaviour {
 public int Minutes = 1;
     void Start() {
 
         yield return new WaitForSeconds(Minutes * 60);
 
     }
 }
Comment
Add comment · Show 2 · 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 yce · Jan 19, 2014 at 11:30 PM 0
Share

I tried but didnt work. Also i didnt find tutorial for c#

avatar image DRProductions yce · Jan 20, 2014 at 12:41 AM 0
Share

Are you sure you applied my script? it should work correctly

avatar image
0

Answer by Lasent · Jan 20, 2014 at 01:50 AM

Unfortunately, in C#, Coroutines aren't that easy to start as in US. Here's how it should look: public int minutes = 1;

 IEnumerator GameOver()
 {
 yield return new WaitForSeconds(minutes * 60);
 Application.LoadLevel("GAMEOVERSCENE");
 }
 
 ...
 
 void Start () //Countdown will begin when you load in the script
 {
 StartCoroutine(GameOver());
 ...
 }

In C#, you explicitly have to set the method IEnumerator, and start it with StartCoroutine.

Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Pause game that not using deltatime for movment 1 Answer

Accurate timing while using timeScale. 1 Answer

Single Step (pause, resume), or externally clock game loop 0 Answers

speed change in standalone compared to editor? 1 Answer

TimeScale = 0 crashes Unity 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges