Question by
oneness19 · Oct 06, 2015 at 12:33 PM ·
toggleswitchtoggle button
toggle button blinks
When I click on the button, the toggle switches, but sometimes 2-3times / click, and the state of button changes that times too of course, the button blinks too, so I randomly get the same result of the toggle as in the beginning.
my question is that, should be this code correct? and is the root of the problem somewhere else in the code? or maybe I need a timer in this, or something else to work this out?
void Update() {
if (coll.collider.gameObject.layer == 9) {
if (Input.GetMouseButtonDown(1) )
{
toggle().active = !toggle().active;
}
}
}
thx Tom and sorry for grammar
Comment
Answer by oneness19 · Oct 06, 2015 at 01:12 PM
Fixed like this:
**float FixTimer=Time.realtimeSinceStartup;**
void Update(){
if (coll.collider.gameObject.layer == 9 )
{
if (Input.GetMouseButtonDown(1) **&& Time.realtimeSinceStartup-FixTimer>0.5**)
{
**FixTimer=Time.realtimeSinceStartup;**
toggle().active = !toggle().active;
}
}