I have this problem and i don't know how to solve it,it's very confusing.I want that every time i instantiate the gameobject it gets a random color, but these clones get the same color from previous one.Like the first prefab spawn and gets random colors, now the next one spawn and olso gets random colors but the first one gets it's color,it's very confusing i know! enter image description here
Here is the code of the gate : public Material[] material; public Color[] colors;
public void ColorChange()
{
colors = new[] {
Color.blue,
Color.black,
Color.red,
Color.green,
Color.yellow,
Color.white,
Color.magenta,
Color.cyan,
Color.grey };
var rnd = new System.Random();
var randomColors = colors.OrderBy(x => rnd.Next()).ToArray();
for (var i = 0; i < material.Length; i++)
{
material[i].color = randomColors[i];
}
}
private void Start()
{
ColorChange();
}
And the script of the spawner: public GameObject Gate; // the gameobject
private float timeBtwSpawn;
public float startTimeBtwSpawn;
public float decreaseTime;
public float minTime = 0.65f;
void Start()
{
}
void Update()
{
if (timeBtwSpawn <= 0)
{
Instantiate(Gate, transform.position, Quaternion.identity);
timeBtwSpawn = startTimeBtwSpawn;
if (startTimeBtwSpawn > minTime)
{
startTimeBtwSpawn -= decreaseTime;
}
}
else
{
timeBtwSpawn -= Time.deltaTime;
}
}