- Home /
On Touch change color
Hi everybody !
I'm trying to make a game for android.
But I'm stuck with this :
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 ?
Answer by robertbu · Nov 05, 2014 at 06:22 PM
It is likely that your shader does not support material.color. See this answer:
http://answers.unity3d.com/questions/795049/oafathow-do-i-changer-the-color-of-a-game-object.html
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().
Answer by mwbranna · Nov 05, 2014 at 06:29 PM
EDIT - Someone beat me to it :(
renderer.material.color(color)
is a shortcut for
renderer.material.SetColor ("_Color", color)
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.
Your answer
Follow this Question
Related Questions
Select 2 out of 3 gameobject before doing something. 0 Answers
Android build weird color changes 1 Answer
Changing level on Android 3 Answers
Tap Control on Android 1 Answer
Please confirm: Galaxy Stylus not readable directly in Unity 1 Answer