Small mistake in my script

Hey,

I’ve made a small script.When i click on a Object,it should change his colour.When i click again it should change the color back.This should go on and on.My script works perfectly.I have only one Problem.The Update function restarts every frame.But one mouse click is longer then one frame.The result is taht the Object start flickering as long as you click.I hope you can help me :frowning:

  var fake = 1;

 
 
 function Update ()
    
    {
    if (Input.touchCount > 0)
    {
    var ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
    var hit : RaycastHit;
    if (Physics.Raycast (ray, hit))
    {
    
    if (hit.collider.tag == "cube"  fake == 1){
      
        red();
          
         }
     
   else {
     green();
     
     }
                    
         
         
     
    }
    }
    }
    
 function red() {
 
    fake++;
    renderer.material.color = Color.red;  
    yield WaitForSeconds(0.5);
    
    }
    
 function green() {
   fake--;
   renderer.material.color = Color.green; 
   yield WaitForSeconds(0.5);
   
   }

Because of your else statement it will continue to flicker once clicked. Are you trying to disable being able to click on an object after it has been clicked on? Or?

I only want that you can click the Object and it changes the color between red and green.So when you click it gets red and when you click again it gets green and when yoou click again it should get red and when you click again it should get green again… :stuck_out_tongue: