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 usename · Dec 21, 2013 at 02:39 AM · c#timerchangefloatreset

Timer variable set to zero

I am trying to make a script that does this. Whenever the game starts and enters the scene containing the script then the variable myTimer will be set to zero and so will the Time.time so that the game can be played again from the start with the time counting up from zero instead of from where the last game ended. This is the script I have but it doesn't do what I want.

 using UnityEngine;
 using System.Collections;
 
 //ATTACH TO MAIN CAMERA, shows your health and coins
 public class GUIManager : MonoBehaviour 
 {    
     public GUISkin guiSkin;            //assign the skin for GUI display
     [HideInInspector]
     public int coinsCollected;
     public float LastTime;
     public float BestTime; 
     public float myTimer;
 
     private int coinsInLevel;
     private Health health;
     
     //setup, get how many coins are in this level
     void Start()
     {
         coinsInLevel = GameObject.FindGameObjectsWithTag("Coin").Length;    
         health = GameObject.FindGameObjectWithTag("Player").GetComponent<Health>();
     }
 
     void Update()
     {
             myTimer = Time.time;
     }
 
     //show current health and how many coins you've collected
     void OnGUI()
     {
         GUI.skin = guiSkin;
         GUILayout.Space(5f);
         if(health)
             GUILayout.Label("Time: " + myTimer.ToString("F2"));
         if(coinsInLevel > 0)
             GUILayout.Label ("Gifts: " + coinsCollected + " / " + coinsInLevel);
         if(coinsCollected > 9)
             Application.LoadLevel("Menu");
             myTimer = 0; 
     }
 }
Comment
Add comment · Show 3
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 Benproductions1 · Dec 21, 2013 at 03:31 AM 0
Share

"but it doesn't do what I want", then what does it do? What isn't working the way it's supposed to work? What have you tried to fix it? Why do you think it doesn't work?

avatar image usename · Dec 21, 2013 at 03:54 AM 0
Share

It simply doesn't count at all anymore with this script.

avatar image usename · Dec 21, 2013 at 03:59 AM 0
Share

I can't set the Time.time to something I decide. If I do it I get this error. error CS0200: Property or indexer 'UnityEngine.Time.time' cannot be assigned to (it is read only)

2 Replies

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

Answer by KellyThomas · Dec 21, 2013 at 07:20 AM

Time.time is readonly.

If you want to track the time since the level started the set a variable to hold the value Time.time returns when gameplay starts.

You can then calculate the difference between this variable and Time.time to determine how much time has passed since.

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 usename · Dec 22, 2013 at 07:50 PM 0
Share

Thanks! under this line if(coinsCollected > 9) how can I add that public float LastTime would then change to the value of the time that has passed? and how can I then change under the same line that BestTime has to change to the value of the time that has passed if the time that has passed is greater than BestTime?

avatar image KellyThomas · Dec 22, 2013 at 09:55 PM 0
Share

Here, this should help get your started:

     private float startTime;
     
     public float levelTime {
         get { return Time.time - startTime; }
     }
     
     void Start() {
         startTime = Time.time;
     }
     
     void Update() {
         Debug.Log(levelTime);
     }
avatar image
0

Answer by MitzeMitchell · Dec 21, 2013 at 03:34 AM

Try setting myTimer/Time.time equal to 0 in the Start function. That way each time this script is called (i.e. whenever you start the level), it will reset the time.

Side note, in your OnGUI function you have several IfThen statements. The Then statements need to be in {}'s. Technically, it will still compile correctly, but sometimes it will count later If's as they were nested within the first.

Hope this helps!

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 Benproductions1 · Dec 21, 2013 at 03:38 AM 0
Share

Please DO NOT post duplicate questions/answers. As per the notification on the top of your screen when you post it, your questions/answers have to go through a moderation queue before they are public.
The only thing you do by posting things multiple times is piss of the mods why look at the queue (in this case me)

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

21 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 avatar image avatar image

Related Questions

timer problem 1 Answer

How to reset timer? (C#) 3 Answers

Multiple Cars not working 1 Answer

Resetting a timer after game over 1 Answer

Distribute terrain in zones 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