- Home /
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
I'm not sure what you trying to do but: Time.fixedDeltaTime ? 0.1f ?
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.
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")
}
}
}
Your answer
Follow this Question
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