Question by
ilias2143 · May 02, 2019 at 11:51 AM ·
c#objectcolorcolor change
How can i get an unique color to each object?
I have 4 objects and each of them has to get a color but the color hast to be unique. How can I change my script to do that?
public class CubeScript : MonoBehaviour { Color[] colors = new Color[4];
// Start is called before the first frame update
void Start()
{
colors[0] = Color.red;
colors[1] = Color.green;
colors[2] = Color.yellow;
colors[3] = Color.blue;
GetComponent<Renderer>().material.color = colors[Random.Range(0, colors.Length)];
}
// Update is called once per frame
void Update()
{
}
}
Comment
Here's some code that'll shuffle a text array. $$anonymous$$odify it to use a colour array. Run this once in start and you'll have a randomised version of your colours which you can just step through and assign.
void reshuffle(string[] texts)
{
// $$anonymous$$nuth shuffle algorithm :: courtesy of Wikipedia :)
for (int t = 0; t < texts.Length; t++ )
{
string tmp = texts[t];
int r = Random.Range(t, texts.Length);
texts[t] = texts[r];
texts[r] = tmp;
}
}
Your answer
Follow this Question
Related Questions
How to make RANDOM COLOR on shader 1 Answer
Is there any way to make a gradient within unity?,Is there any way to make a gradient? 1 Answer
Why does my random colour generator always spit out the same colour? 0 Answers
Color.Lerp over HP 1 Answer
Image Colour change not working with anythign other than base preset colours 2 Answers