Hi I've been trying to find a way to instantiate a prefab with a random color. I have looked at many questions with similar titles I'm sure they are good answers but they are not what I need. I am making a replica of a game, for practice and I want to have to random colors to be used when instantiating an object here is my code:
private Vector3 spawnPos = new Vector3(0, 0.75f, 0);
[SerializeField]
private GameObject hitCube;
private Color color1;
private Color color2;
private string lastColor;
// Use this for initialization
void Start () {
color1 = Random.ColorHSV(0f, 1f, 0f, 0f);
color2 = Random.ColorHSV(0f, 1f, 0f, 0f);
for (int i = 0; i < 21; i++)
{
if (lastColor == "color1")
{
hitCube.GetComponent<Renderer>().sharedMaterial.color = color2;
lastColor = "color2";
}
else
{
hitCube.GetComponent<Renderer>().sharedMaterial.color = color1;
lastColor = "color1";
}
Instantiate(hitCube, spawnPos, Quaternion.identity);
spawnPos.y += 0.50f;
}
}
So does anyone know how I can make this work? When I use this the ground becomes one color and so do all of the cubes.
Another question I have is why does the ground change color but the launcher doesn't (it looks like it does but actually it is already set to be a darker shade of grey), and why does the ground change color in the first place? I understand that
sharedMaterial
is not the way to go because it is for all of the prefabs. All help is appreciated.
Aucun commentaire:
Enregistrer un commentaire