- Home /
Random Color
Hi I am trying to change the color randomly by calling a function but I can't seem to figure out how to even do it. I am new to Developing so I don't know much of anything but if you do help me please explain how you are doing for I can understand and learn :)
function BallColor ()
{
var Color1 : int;
var Color2 : int;
var Color3 : int;
var Color4 : int;
Color1 = Random.value (201, 250);
Color2 = Random.value (201, 250);
Color3 = Random.value (201, 250);
Color4 = Random.value (201, 250);
gameObject.renderer.material.color.(Color1,Color2,Color3,Color4);
}
Answer by AlucardJay · Jan 08, 2015 at 01:56 AM
http://docs.unity3d.com/ScriptReference/Color.html
Each color component is a floating point value with a range from 0 to 1.
Color1 = Random.value (0f, 1f);
please format your code
I'm trying this but it isn't working.
function BallColor ()
{
var Color1 : float;
var Color2 : float;
var Color3 : float;
Color1 = Random.value(0f, 1f)
Color2 = Random.value(0f, 1f)
Color3 = Random.value(0f, 1f)
gameObject.renderer.material.color = Color(Color1,Color2,Color3);
}
but it isn't working really doesn't give any information. Is it an error? What is the message in the console? In your above code you are missing the ; at the end of the Color lines.
Not only that the ; are missing, but a color consists of four 0-1 values: Red Green Blue Alpha (Transparency, used by Transparent Diffuse shaders for example).
Your color is missing the alpha component, so it has to be
gameObject.renderer.material.color = Color(Color1,Color2,Color3, 1);
Edit: Color does have a rgb constructor. Thanks to alucardj for the notice.
Color does have a constructor for r g b values, and assumes a=1 :)
In that case, I bow to your superior knowledge and will edit out my earlier comment. Thanks :)
Your answer
Follow this Question
Related Questions
[Resolved] Game view displays the wrong colors? 1 Answer
i'm trying to code a premise, if 3 objects are the same color then it will destroy the wall 1 Answer
Switch between random colors 4 Answers
Using predefined colors vs defining new colors 1 Answer
How do I randomly create 2 objects, each with a different shape and color? 1 Answer