Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Tageos · Jul 27, 2015 at 03:58 PM · c#timer

Start timer on mouse0 click

I want to start a timer when i click mouse0 (lmb), so that on only one click the timer starts counting.

What i have now only adds a few splits of a second to the timer for every click. It does not "count up".

 if (Input.GetKeyDown (KeyCode.Mouse0))
         {
             isWithdrawing = true;
             WithdrawTimer += Time.deltaTime;
         }

The variables used are public so i can confirm it.

Any thoughts on how to make it count up fluently when i click the mouse0 key?

Thank you

Comment
Add comment · Show 2
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 Positive7 · Jul 27, 2015 at 04:11 PM 0
Share

I'm not sure what you trying to do but: Time.fixedDeltaTime ? 0.1f ?

avatar image Tageos · Jul 27, 2015 at 04:15 PM 0
Share

I just want to have a timer that starts counting up when i click lmb, now it just adds a number to the timer when i click lmb. perhaps i was not clear enough.

1 Reply

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

Answer by Hexer · Jul 27, 2015 at 04:24 PM

I added another bool and put the timerlogic in a function of its own. On mouse click the bool is turned true and wil InvokeRepeat the TimerLogic. When you want the timer to stop you have to put bool"X" to false.

  using UnityEngine;
     using System.Collections;
 
 public class Timer : MonoBehaviour {
 
     bool isWithdrawing; 
     bool X = false;
     float WithdrawTimer;
     public GUIText timerText;
 
     // Use this for initialization
     void Start () {
     
     }
 
     void TimerLogic(){
         isWithdrawing = true;
         WithdrawTimer += Time.deltaTime;
     }
     
     // Update is called once per frame
     void Update () {
         timerText.text = WithdrawTimer.ToString ();
     
         if (Input.GetKeyDown (KeyCode.Mouse0))
         {
             X= true;
         }
 
         if (X == true) {
             InvokeRepeating("TimerLogic",0.1f,60.0f);
         }
 else 
 {
 CancelInvoke("TimerLogic")
 }
     }
 }


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 Positive7 · Jul 27, 2015 at 04:30 PM 0
Share

Erm.. You were faster.

avatar image Tageos · Jul 27, 2015 at 04:45 PM 0
Share

Thank you!

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

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

timer problem 1 Answer

Convert this string formatting from C# to JS 1 Answer

I want to have cancelinvoke start multiple seconds arfter object becomes invisible 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