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 rOBY GAMES · Jan 26, 2014 at 10:40 PM · scenegametimelevelscrip

Problem stopwatch start the game.

Hello to all!

I am new to scripting. When I try the game finished, in the menu I select play game now everything is ok, but when the game starts the clock has already started scoring 1 or 2 minutes already elapsed. I would like the stopwatch 0:0:0 departed from, when I press play on the menu, how to solve this problem?

stopwatch script I am using.

 var start;
 var tempoRimanente:int;
 var millisecondi:int;
 var secondi:int;
 var minuti:int;
 
 function Start() {
  
      start=120; //secondi;
 
 }
 
 function Update () {
 
    tempoRimanente= start = Time.time;
    minuti = tempoRimanente/60;
    secondi = tempoRimanente%60;
    millisecondi = tempoRimanente%10;
      
      
     
       if(tempoRimanente<0)
    guiText.text="GAME OVER";
    else
    guiText.text= minuti.ToString("D2") + ":" + secondi.ToString("D2") + ":" % millisecondi.ToString("D2");
 
 }
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
Best Answer

Answer by Drakulo · Jan 27, 2014 at 09:25 AM

The readonly Time.time var contains the time elapsed since you started Unity3D engine. So to manage your level time, you have to do some more code.

When your press the "Play" button, store the actual elapsed time in your script. This will be your start point :

 var levelStartTime = Time.time;

Now, include this var in your time computation. The remaining level time will be :

 var remainingTime = start + levelStartTime - Time.time;
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 rOBY GAMES · Jan 27, 2014 at 06:09 PM 0
Share

Thank you for having responded to me!

I do not understand a lot of scripts. :-( Where dorei insert these variables in the script?

I'll show you how I did. so is it wrong? Please help me!

 var levelStartTime = Time.time;
 var remainingTime = start + levelStartTime - Time.time;
 
 var start;
 var tempoRimanente:int;
 var millisecondi:int;
 var secondi:int;
 var $$anonymous$$uti:int;
 
 function Start() {
  
  
     
      
      //levelStartTime=Time.time; //secondi;
 
 }
 
 function Update () {
 
    
    
    tempoRimanente= start = Time.time;
    $$anonymous$$uti = tempoRimanente/60;
    secondi = tempoRimanente%60;
    millisecondi = tempoRimanente%10;
      
      
     
       if(tempoRimanente<0)
    guiText.text="GA$$anonymous$$E OVER";
    else
    guiText.text= $$anonymous$$uti.ToString("D2") + ":" + secondi.ToString("D2") + ":" % millisecondi.ToString("D2");
 
 }
avatar image Drakulo · Jan 27, 2014 at 11:02 PM 0
Share

Ok let's keep it simple then, in only one script. This may not be the best solution but it's simple.

You will have a boolean storing if the game is on or off. When the flag is true, the timer will go on. When it's false, the menu is shown. The objective is to set the start time when the player click the start button (I guess you're using the unity GUI system). By clicking on the button, the start time will be set up and the playing flag set to true. The Update function manages your game logic until the player win or until the timer goes out. When the game is over, just flip the playing flag back to false.

 var levelTimer = 120; // The level timer
 var playing = false; // Not playing by default
 var levelStartTime = 0f; // The current level start time
 
 function OnGUI(){
     if(!playing){
         // The GUI is shown only if the game is not started
 
         // Fill this rect with your button position & size
         var buttonPosition = new Rect(x, y, width, height); 
         if(GUI.Button(buttonPosition, "Play")){
             // Here you have to initialize the start time
             levelStartTime = Time.time;
             playing = true;
         }
     }
 }
 
 function Update(){
     if(playing){
         // Compute the remaining time
         var remainingTime = levelStartTime + levelTimer - Time.time;
 
         // Add your game loop code here
 
         // Replace theGameIsOver by your gameover conditions
         if(theGameIsOver){
             playing = false;
         }
     }
 }
avatar image rOBY GAMES · Jan 28, 2014 at 07:31 PM 0
Share

Thank you Drakulo!

Are you the number one scripter. Thank you so much! :-)

avatar image Drakulo · Jan 28, 2014 at 07:36 PM 0
Share

I don't think so but... thanks. Don't forget to set the answser as accepted. Happy gamedev with Unity3D !

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

19 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

Related Questions

Change Scene after time? 1 Answer

Calling a function or Object in a level 1 Answer

Change scenes from area? 1 Answer

UI Button deletes Function on start 1 Answer

Objects are visible through terrain 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