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 PiffPaff · Feb 18, 2012 at 07:25 PM · scenetimercounterorder

Timer gets executed in the wrong scene

Hello.

I started using Unity 2 days ago and have created a small "Break Out" type of game. I now have a problem with a timer script: Apparently it gets executed in the wrong scene or globally for some reason.

For now, I have 2 scenes: One game scene which contains the timer. It is displayed on the screen as a GUI text and counts down from 2:00 minutes to 0:00. The clockscript (see below) is attached to a Game Object ("Game Object Clock") that also includes the GUItext component.

I added another scene to the game, which is a splash title screen. It has a "click to play" GUI button which leads to the game scene.

For some reason, the clockscript seems to run immediately after the game starts, i.e. during the title screen. Once I click the play button, the game scene is loaded with a certain amount of time already substracted from the countdown (= exactly as long as I had the title screen running before clicking the button).

Information:

  • the script is not attached to any object in the title screen scene.

  • the "Game Object Clock" is not part of the hierarchy in the title screen scene at all.

  • the debug log message from the clock script does appear during the game scene just fine, but it does NOT appear during the title screen. This leads me to the conclusion that only part of the script is running prematurely. Or maybe not...

Stuff that has not worked to correct the issue:

  • replacing the Start () function with a different function name or leaving the Start () function empty.

  • applying the clockscript to a Game Object or the Main Camera in the title screen scene and setting it to "pause"

  • trying to delay the script in "execution order" (clock script inspector settings)


How can I keep the clock from invisibly ticking away during the title screen and make it start at exactly 2:00 only when the actual game level starts? Or is there maybe a way to reset it upon loading the right level?

It's probably something really easy and trivial, but nothing I try seems to work or even affect the script in any way. Any help is appreciated.


clockscript code:

 var isPaused : boolean = false;
 var startTime : float; //(in seconds)
 var timeRemaining : float; //(in seconds)
 
 
 function Start() {
     startTime = 120.0;
 }
 
 function Update() {
     if (!isPaused)
         {
             DoCountdown();
     }
 }
 
 function DoCountdown() {
     timeRemaining = startTime - Time.time;
     Debug.Log("time remaining = " + timeRemaining);
         
     if (timeRemaining < 0)
     {
         timeRemaining = 0;
         isPaused = true;
         TimeIsUp();
     }
     ShowTime();
 
 }
 
 function PauseClock() {
     isPaused = true;
 }
 
 function UnpauseClock() {
     isPaused = false;
 }
 
 function ShowTime() {
     var minutes : int;
     var seconds : int;
     var timeStr : String;
     minutes = timeRemaining/60;
     seconds = timeRemaining % 60;
     timeStr = minutes.ToString() + ":";
     timeStr += seconds.ToString("D2");
     guiText.text = timeStr; //display the time to the GUI
 }
 
 function TimeIsUp() {
 Debug.Log("Time is up!");
 }
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
0

Answer by PiffPaff · Feb 20, 2012 at 03:21 AM

Hey PiffPaff, I figured this out for you:

Time.time needs to be replaced with Time.timeSinceLevelLoad

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Need help with switch case in order/timed 0 Answers

Timer that can be accessed in multiple scenes 1 Answer

Movement in my terrain ? 2 Answers

Scene Audio Script Error 2 Answers

Adding commands to buttons? 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