Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Zombie_Socks_88 · Nov 30, 2012 at 04:23 PM · javascriptbeginnertimerdelay

Need help with delaying a timer (javascript)

Ok I currently have my scenes running like this.

Main Menu
|
Player Select
|
Level 1

In the Level 1, I have a script (Level1InfoScript) that has a timer that countsdown and when it hits zero it will end the level (haven't written that part yet). My problem is that the timer starts counting down right when the game is launched on the main menu, instead of waiting until Level1 is loaded to countdown. Making it look like this.

Main Menu(this is when the timer starts)
|
Player Select
|
Level1 (this is where the timer is suppose to start)

I'm very new to programming and stuff, so any help or advice would be great. I looked on here before asking, and found a somewhat similar question, but the advice given in that wasn't working, so that why I am now here myself.

Here is the code. I got most of the code from a Unity Beginners Guide book and building from there. I have omitted stuff that doesn't pertain to the timer itself.

 var isPaused : boolean = false;
 var startingTime : float; // This is in seconds
 var timeRemain : float; // Also in seconds
 
 function Start()
 {
     startingTime = 20.0;
 }
 
 function Update ()
 {    
     if(!isPaused)
     {
         //To make sure the time is not paused
         StartCountdown();
     }
 }
 
 function StartCountdown()
 {
     timeRemain = startingTime - Time.time;
     if(timeRemain < 0)
     {
         timeRemain = 0;
         isPaused = true;
         TimeUp();
     }
     ShowTime();
 }
 
 function ClockPause()
 {
     isPaused = true;
 }
 
 function ClockUnpause()
 {
     isPaused = false;
 }
 
 function ShowTime()
 {
     var minutes : int;
     var seconds : int;
     var timeStr : String;
     
     minutes = timeRemain / 60;
     seconds = timeRemain % 60;
     timeStr = minutes.ToString() + ":";
     timeStr += seconds.ToString("D2");
     guiText.text = timeStr; //shows the time in the GUI
 }
 
 function TimeUp()
 {
 
 }
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

2 Replies

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

Answer by Griffo · Nov 30, 2012 at 06:33 PM

Just change your line

 timeRemain = startingTime - Time.time;

To

 timeRemain = startingTime - Time.timeSinceLevelLoad;
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 Zombie_Socks_88 · Nov 30, 2012 at 07:50 PM 0
Share

Awesome that worked. Thanks.

avatar image Griffo · Nov 30, 2012 at 07:59 PM 0
Share

Glad I could help, can you mark it as answered please, thanks.

avatar image
0

Answer by BiG · Nov 30, 2012 at 04:28 PM

Maybe your script is assigned to an object that exists in all the levels (so, also in Main Menu and Player Select), even if you have no need for that in those levels.

However, supposing that you have the following index structure:

 LEVEL NAME            LEVEL INDEX
 
 Main Menu             0
 
 Player Select         1

 Level 1               2

 ...

 Level n               n+1


, you could modify the Update() as follows:

 function Update ()
 {   
     if ((!isPaused) && Application.loadedLevel > 1)
     {
        //To make sure the time is not paused
        StartCountdown();
     }
 }

That is: if the game isn't paused AND you have passed the Main Menu and Player Select screens (so, you are at least in Level 1), launch StartCountDown().

Comment
Add comment · Show 1 · 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 Zombie_Socks_88 · Nov 30, 2012 at 05:19 PM 0
Share

I went in and checked to make sure the other scenes don't have the script and they don't. So I added that code and it's still counting down during the main menu and player select.

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

13 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

Related Questions

Basic toggle timer Help! 2 Answers

Random Delay time 2 Answers

Setting Scroll View Width GUILayout 1 Answer

Best Unity Practices 1 Answer

gameoverscript.js(1,1): BCE0044: expecting EOF, found 'gameOver'. 3 Answers


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