I tried to generate a random color like this:
Color background = new Color(
(float)Random.Range(0, 255),
(float)Random.Range(0, 255),
(float)Random.Range(0, 255)
);
But I always get white as result.
What am I doing wrong?
I tried to generate a random color like this:
Color background = new Color(
(float)Random.Range(0, 255),
(float)Random.Range(0, 255),
(float)Random.Range(0, 255)
);
But I always get white as result.
What am I doing wrong?
UnityEngine.Random.ColorHSV()
generate Random color
As suggested by Dragate, the Color
struct needs values between 0 and 1, as indicated in the documentation
Representation of RGBA colors.
This structure is used throughout Unity to pass colors around. Each color component is a floating point value with a range from 0 to 1.
In order to use values between 0 and 255, you need the Color32 class :
Color32 background = new Color32(
Random.Range(0, 255),
Random.Range(0, 255),
Random.Range(0, 255),
255
);
// Color version
Color background = new Color(
Random.Range(0f, 1f),
Random.Range(0f, 1f),
Random.Range(0f, 1f)
);
This is the simplest:
// Pick a random, saturated and not-too-dark color
Color color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
Color background = new Color(
Random.Range(0f, 1f),
Random.Range(0f, 1f),
Random.Range(0f, 1f)
);
Maybe try this:
string seed = Time.time.ToString ();
System.Random random = new System.Random (seed.GetHashCode ());
Color background = new Color(
(float)random.Next(0, 255),
(float)random.Next(0, 255),
(float)random.Next(0, 255)
);
Sweet AmmarSalim…i just needed to specify the renderer when attached to my gameObject
. But yeh your code worked. Big up ! Thanks
obj.GetComponent.material.color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
Sweet AmmarSalim…i just needed to specify the renderer when attached to my gameObject. But yeh your code worked. Big up ! Thanks
obj.GetComponent().material.color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
public Color32 RandomColorGenerator()
{
byte r = (byte)Random.Range(0, 255);
byte b = (byte)Random.Range(0, 255);
byte g = (byte)Random.Range(0, 255);
Color32 color = new Color32(r,b,g,255);
return color;
}