Change Variable and Color when click on Objekt

Hay,

When i click on a Specific Gameobject,it should change it’s Material Colour and a Variable.
For example,when i click on a blue cube,it should get red and when i click again it should be blue an when i click again it should be red… :smile:

But my script is not working :frowning:

static var color : float = 1;
 function Start ()
 {
 renderer.material.color = Color.red;
 }
 
 function Update ()
    
    {
    if (Input.touchCount > 0)
    {
    var ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
    var hit : RaycastHit;
    if (Physics.Raycast (ray, hit))
    {
    Debug.Log(" You just hit " + hit.collider.gameObject.name);
    if(hit.collider.tag == "cube"){
      if(color = 1){
         renderer.material.color = Color.blue;
         color =0;
         }
      if(color = 0){
        renderer.material.color = Color.red;   
        color =1;
        }
    
    }
    }
    }
    }

does your gameobject has a collider attached?

btw you can also use onmousedown to register klicks.