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 mikhailovic · Jul 11, 2014 at 06:32 PM · timerguitextonoff

How can i make my Timer script start and pause only when something certain is activated?

Hiya guise. I have a script here that works like a timer. And essentially i want it to only record when my NightVision is enabled (when you press N button), and when you disable nightvision the timer pauses, to start from where it was when you enable nightvision again etc. How can i go about doing this? Suggestions would be much appreciated!

CODE:

 #pragma strict
 
 private var startTime : float;
 var textTime : String;
 
 function Start ()
 {    
     startTime = Time.time;
 }
 
 
 
 function OnGUI ()
 {
     
         var guiTime = Time.time - startTime;
         
         var minutes : int = guiTime / 60;
         var seconds : int = guiTime % 60;
         var fraction : int = (guiTime * 100) % 100;
         
         textTime = String.Format ("REC {0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
         GetComponent(GUIText).text = textTime;
         GetComponent (GUIText).color = Color.red;
         GetComponent (GUIText).pixelOffset = Vector2 (-480, 250);
         GetComponent (GUIText).fontSize = 25;
 }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by MonkeyHood · Jul 11, 2014 at 07:17 PM

Instead of " var guiTime = Time.time - startTime;", try adding Time.deltaTime.

deltaTime is time since last frame.

Also, I don't really know JS but the logic is sound. I'm more used to c#.

var time = 0; var timerOn = true; //this should be set to true or false when N is pressed

function OnGUI() { if(timerOn == true) time += Time.deltaTime; }

Comment
Add comment · Show 3 · 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 mikhailovic · Jul 11, 2014 at 08:37 PM 0
Share

I dont really know how to implement it in my code, would be happy if someone could explain further. Good points though!

avatar image Kiwasi · Jul 11, 2014 at 09:03 PM 0
Share

Please format your code

avatar image mikhailovic · Jul 12, 2014 at 08:12 PM 0
Share

Can annyone help?

avatar image
0

Answer by tconti · Jul 13, 2014 at 12:03 AM

you dont want to know startTime - current time what you want is to increase the timer during the Update() or onGUI() method JS isnt my strong suit but i hope i get my point across

 function Start(){
     timer = 0;
 }
 
 function onGUI () {
     if(nightVisionIsActive){
         timer += Time.deltaTime; //<--this is the amount of time since last frame
     }
 }

you need to keep track of when you are in night vision, so just toggle the bool in onKeyDown() then the timer will only count up when it is active

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 mikhailovic · Jul 13, 2014 at 11:38 AM 0
Share

Feel free to edit in my code because im not that good with syntaxes yet.

avatar image mikhailovic · Jul 14, 2014 at 08:06 PM 0
Share

Hi i edited my code to be able to make the timer stop when i press a button. But how come Time.time only works when its inside OnGUI and not when its declared as "x" ?? I mean that it doesnt start counting. Pls need some help!

CODE:

 #pragma strict
 
 private var startTime : float;
 var REC : GameObject;
 var textTime : String;
 var x = Time.time;
 
 function Start ()
 {    
     REC.guiText.enabled = false;
     startTime = 0;
 }
 
 function Update ()
 {
     if (Input.Get$$anonymous$$eyDown("n"))
     {
         if (REC.guiText.enabled == false)
         {
             REC.guiText.enabled = true;
             
         }
         else
         {
             REC.guiText.enabled = false;
         }
     }
 }
 
 function OnGUI ()
 {
     var guiTime = x;
     var hours : int = guiTime / 3600;
     var $$anonymous$$utes : int = guiTime / 60;
     var seconds : int = guiTime % 60;
     var fraction : int = (guiTime * 100) % 100;
     
     textTime = String.Format ("REC {0:00}:{1:00}:{2:00}:{3:00}", hours, $$anonymous$$utes, seconds, fraction);
     GetComponent(GUIText).text = textTime;
 }

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

switch game object on of by trigger with VR 0 Answers

GUIText not working properly with a timer. 1 Answer

Help with timer guitext 2 Answers

UI Button ON/Off 3 Answers

On/off switch from server to clients live 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