• 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 06:09 AM · c#guitimerreferencefloat

timer problem

Whenever the game starts and enters the scene that is 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 have just described. How can this be done?

 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; 
     float time = Time.time - Time.time;
     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>();
         myTimer = Time.time - Time.time;
     }
 
     void Update()
     {
         myTimer = 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");
     }
 }
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 highpockets · Dec 21, 2013 at 07:05 AM

You have the variable time = Time.time - Time.time; declared at the top, outside any function. That will return 0.

In the Start function, you also have another variable, myTimer = Time.time - Time.time; That will return 0,

Then you assign the variable, time to myTimer. That is the same as 0 = 0…

I'm pretty sure you wound up with 0 over and over again on your GUI label, right?

Time.time is read only, so you cannot change it, but if you just assign Time.time to the myTimer variable in the start function, then you can subtract it from Time.time to find the time that has passed since you started.

 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;
     float myTimer;
     float newTime;
  
     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>();
        myTimer = Time.time;
     }
  
     void Update()
     {
        newTime = Time.time - myTimer;
     }
  
     //show current health and how many coins you've collected
     void OnGUI()
     {
        GUI.skin = guiSkin;
        GUILayout.Space(5f);
        if(health)
          GUILayout.Label("Time: " + newTime.ToString("F2"));
        if(coinsInLevel > 0)
          GUILayout.Label ("Gifts: " + coinsCollected + " / " + coinsInLevel);
        if(coinsCollected > 9)
          Application.LoadLevel("Menu");
     }
 }
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Distribute terrain in zones 3 Answers

is it cheaper to run timer another, or refer to other timer? 1 Answer

Multiple Cars not working 1 Answer

Timer variable set to zero 2 Answers

C# countdown timer 9 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