- Home /
how to reset a float var
i want to reset my timer OnMouseDown thats it simple if any 1 could help me i would be very greatful tnx. here is the script:
var timer : float = 0;
var reset : float = 0;
var speed : float = 0.2;
function Update () {
timer++;
if(timer == 500)
{
this.transform.position.x -= this.speed;
}
}
function OnMouseDown () {
//here i would like to reset the var timer
}
i hope some 1 can help me
What does it mean "reset"? It's starting as 0, so just set it to 0 (timer = 0;). Anyway checking for timer until it's 500 in Update can produce unexpected results, since the framerate can potentially severely affects the number of times it's increased (it's not like "wait 500 milliseconds or 500 seconds" or anything like that).
Answer by Antony-Blackett · Dec 13, 2014 at 04:10 PM
function OnMouseDown () { timer = 0; }
i apiriciat the efort but it dosent realy work tnx any ways
Then something else is going wrong cause setting a variable to 0 should be that simple. Put a Debug.Log("Am I working?" ); call inside that function and see if On$$anonymous$$ouseDown() is even being called.
http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.On$$anonymous$$ouseDown.html
Does your object have a collider or GUIElement attached to it? Because the documents say you need that for On$$anonymous$$ouseDown() to be called.
dude im so sory i just realized what was wrong im so sory there was a invis button blocking The way so i couldent press the gameobject whit the script tnx for the answer thank you and im so sory and realy greatfull thank you.
tnx again helpd me realy (im a bigginer if you couldent tell alredy)
Answer by Ericool · Dec 14, 2014 at 09:06 AM
Usually ++ is for an integer. Secondly , what you want to do is moving around a gameObject so use a simple state machine : if(transform.position < 0.0f) state = goright; else if(transform.position > 500.0f) state = goleft;
if(state == goleft) t = 1.0f;
else t = -1.0f;
transform.position += t * Time.deltaTime * (Vector.right);
something like that.
Your answer
![](https://koobas.hobune.stream/wayback/20220613173949im_/https://answers.unity.com/themes/thub/images/avi.jpg)