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 RamboPanda · Sep 13, 2011 at 05:28 AM · iostimereset

Resetting Time after Game Reload in iOS

How do I reset the time after the game fail? The time just keeps decreasing in my game even after the game over scene is played out and it restarts back to the main scene.

This is my script attached to the GUI for time:

 #pragma strict
 
 private var startTime;
 var restSeconds : int;
 private var roundedRestSeconds : int;
 private var displaySeconds : int;
 private var displayMinutes : int;
 
 
 
 
 var countDownSeconds : int;
 
 function Awake() {
     startTime = countDownSeconds;
     
 }
 
 function Update () 
 {
     guiText.text = String.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds); 
 }
 
 function OnGUI () {
   
     var startTime: float;
     var guiTime: float = Time.time - startTime;
 
     restSeconds = countDownSeconds - (guiTime);
 
     
     if (restSeconds == 60) {
         print ("One Minute Left");
     }
     if (restSeconds == 0) {
         print ("Time is Over");
         LevelFail();
         
     }
 
     
     roundedRestSeconds = Mathf.CeilToInt(restSeconds);
     displaySeconds = roundedRestSeconds % 60;
     displayMinutes = roundedRestSeconds / 60; 
 
 }
 
 function LevelFail()
 {
     Application.LoadLevel("GameOver");
     startTime = 240;
 }

My countDownSeconds is set to 240 in the inspector. Thank you! :)

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
2
Best Answer

Answer by syclamoth · Sep 13, 2011 at 11:05 AM

You are counting down using Time.time, instead of detracting Time.deltaTime every frame. The problem here is that Time.time is absolute- which means that no amount of level loading and resetting will set it back to zero: just like in real life, time that is lost can never be recovered. Instead, you should put a countdown float at the top and decrease it by Time.deltaTime inside the Update function, then base all of your GUI stuff off it. Another thing, by redefining startTime at the top of OnGUI, you are actually hiding the startTime var elsewhere in your program, which I don't think you really want to do.

Comment
Add comment · Show 8 · 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 RamboPanda · Sep 14, 2011 at 12:33 AM 0
Share

Sorry I don't really understand. Did you mean it like that?

function Update () { startTime= Time.deltaTime - countDownSeconds; guiText.text = String.Format ("{0:00}:{1:00}", display$$anonymous$$inutes, displaySeconds); }

But time did not re-set... As for re-defining the startTime, should I declare at the top or OnGUI() is fine? Thank you for helping. Cheers!

avatar image syclamoth · Sep 14, 2011 at 03:34 AM 0
Share

Time.time is a counter of the total number of seconds since the beginning of the game. If you define your countdown in terms of it, the countdown will continue to decrease even if you reset the level! Time.deltaTime, on the other hand, is the amount of time between the last time Update was called and now- so if you have a countdown float countdown, and then use

countdown -= Time.deltaTime;

once every Update, then it will decrease at a constant rate of one per second. This is a much more general way of implementing a countdown, and will reset automatically when you load the level!

avatar image RamboPanda · Sep 14, 2011 at 03:40 AM 0
Share

But my countdown is an integer cause I want to set a fix 4 $$anonymous$$ute for the game.

avatar image syclamoth · Sep 14, 2011 at 04:14 AM 0
Share

Why does the countdown have to be an int? I don't understand this. There is no reason why it can't be a float ins$$anonymous$$d- if there's any time when you absolutely need it to be an integer you can just use $$anonymous$$athf.RountToInt(countdown) to turn it into an int temporarily.

avatar image RamboPanda · Sep 14, 2011 at 04:18 AM 0
Share

Because the time starts at 4$$anonymous$$ and I would like to re-set it to 4$$anonymous$$ each time the scene loads.

Show more comments

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

Time based scoring 2 Answers

Screensaver when device is idle 1 Answer

How do you change a sprite after an amount of time? 0 Answers

ResetTime doesnt work. help pls 1 Answer

Time.timeSinceLevelLoad does not reset 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