I’ve a rotating cube with is trigger box collider for testing and I’m trying to change his color material (A simple white texture) with this code attached to it :
var colors : Color[];
function Update ()
{
if (Input.touchCount > 0)
{
var hit : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint((Input.GetTouch(0).position)), Vector2.zero);
if(hit.collider != null)
{
renderer.material.color = colors[Random.Range(0, colors.Length)];
}
}
}
But nothing happen when i click/touch on play mode and on unity remote !
Any idea ?
It is likely that your shader does not support material.color. See this answer:
Note the code you have here will change the color randomly for every frame that your finger is touching some object. While I’m only guessing, consider using OnMouseDown() and getting rid of Update() and the Raycast().
If the material you are using doesn’t have a “Color” field, then the first method won’t work. This field could be something like “_TintColor”, and can be looked up in the inspector.