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 Omer.Hussain · Feb 21, 2014 at 11:48 AM · timertime.deltatimetimescaletime.time

timer starts in the menu scene

Hi! i am middle of something... what is happening in my game, i have two scenes, scene1 and scene2 , i have a script of timer which actually is the score which should start when i load the scene2 as in the temple run ,subway surfer etc. when i play my game first it show the menu scene then we hit play and move to scene2, now what is happening when game load scene1 starts, my timer score starts counting time score in the scene1 although my script and score gui is in the scene2. and when my player collide and we go back to scene1 the score is running and after again loading the scene2 the scores are like 1000....1950..what i want to just to start my score in the scene2 and after reloading scene2 start form the zero. #pragma strict

 private    var startTime:float;
 //    var labelPosition : Rect;
     var labelText : String;
     var labelStyle : GUIStyle;
     var score : int;
 static    var timerstart:boolean = true;
 
     function Update()
     {
         if (timerstart)
         {
         startTime= Time.deltaTime;
         //startTime= 
         labelText = startTime.ToString();
         }
     }      
     function OnGUI()
     {
         
         GUI.Label(Rect (Screen.width-125, 30, 130, 25), labelText, labelStyle);
     }
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
Wiki

Answer by valerik · Feb 21, 2014 at 12:09 PM

maybe you need PlayerPrefs

Full details are here: http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html

use something like this:

 static var timeSpentInSceneX: float;
  
 function saveTimer(){
  PlayerPrefs.SetFloat("timerSceneX",timeSpentInSceneX);
 }
  
 function getTimer(){
  storedTimer = PlayerPrefs.GetFloat("timerSceneX");
 }

and then use some operation to modify your timer value! ex:

 timeSpentInSceneX = Time.deltaTime;
 function saveTimer();

(maybe Time.time it's better, it's frame indipendent)

then you store your time as above, when you load a new scene you get the old stored value and you re-do:

 timeSpentInSceneX = Time.deltaTime;
 float totalTime = timeSpentInSceneX + getTimer();
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 Omer.Hussain · Feb 24, 2014 at 06:44 AM 0
Share

Thanks for your response @valerik ... should i apply this script in the scene2 timer empty object , bacause i have applied my above script in the same manner. and i am using time.time now. and how in your script i will apply the GUI.label to show the time.... kindly write that in your code. i am very new to unity and js coding. in my script if i change startTime:int and deltatime into time.time it shows time in the scene2 well, but as i mentioned the timer starts in the scene1. this is the main concerned ... i want it start it in the scene2.. :( ... kindly help .. and playerpref is furute work but if i cover it in here i ll be more glade .. bacuase i will have to save the time as well to show on the scene3 gui..... ;p

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

Time.deltaTime returns smaller values than actual time between frames 0 Answers

How do I make the timeScale not affect the timer? 1 Answer

Pause & Unpause Different timers 1 Answer

How to limit an effect to a certain duration 2 Answers

Delay Player From Using Action Button For A Period Of Time 0 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