- Home /
OnMouseEnter random hover color
i would like to know how to set a random color for a mouse hover here is my first try with random, is my array wrong?
// old : renderer.material.color = Color.gray;
var arrayHovercolor = new Array(renderer.material.color);
arrayHovercolor.Push (Color.gray);
arrayHovercolor.Push (Color.green);
arrayHovercolor.Push (Color.blue);
arrayHovercolor.Push (Color.red);
arrayHovercolor.Push (Color.white);
arrayHovercolor.Push (Color.black);
arrayHovercolor.length = 6;
var randomColor = renderer.material.color;
randomColor = arrayHovercolor(Random.Range(0,arrayHovercolor.length));
print("i hover here"); //debug
Answer by Mike 3 · Feb 10, 2011 at 01:27 PM
These should fix it:
//you used a colour here - it only takes an int for size
var arrayHovercolor = new Array();
and
//square brackets for accessing array items randomColor = arrayHovercolor[Random.Range(0,arrayHovercolor.length)];
//don't forget to set the material's colour to the picked color renderer.material.color = randomColor;
You also don't need this line, so add var to the randomColor = line above:
var randomColor = renderer.material.color;
Answer by Jesse Anders · Feb 10, 2011 at 10:15 AM
Try:
renderer.material.color =
arrayHovercolor(Random.Range(0,arrayHovercolor.length));
(Assuming UnityScript doesn't differ from C# in this respect, Color is a value type rather than a reference type, so you can't acquire a reference to it as you appear to be trying to do currently.)
thank you, i try this in my java-script, but unity don't compile and give me this : "It is not possible to invoke an expression of type 'Array'."
You most likely need square brackets there rather than parentheses, i.e. arrayHovercolor[...].
thank you, i try this, but unity don't compile again : Cannot convert 'UnityEngine.Color' to 'int'.
Answer by cacysunlee · Feb 10, 2011 at 01:53 PM
thank you very much now i solved it :
function OnMouseEnter(){
var arrayHovercolor = new Array();
arrayHovercolor.Push (Color.gray);
arrayHovercolor.Push (Color.green);
arrayHovercolor.Push (Color.blue);
arrayHovercolor.Push (Color.red);
arrayHovercolor.Push (Color.white);
arrayHovercolor.Push (Color.black);
renderer.material.color = arrayHovercolor[Random.Range(0,arrayHovercolor.length-1)];
print("i hover here"); //debug
}
Your answer
Follow this Question
Related Questions
Change text color on GUI.Label 1 Answer
Random colors for particles? 0 Answers
C# Randomize Background.Color Issues 1 Answer
random Color. Glitch?!? 1 Answer
Random objects don't overlap 1 Answer