- Home /
Question by
theLightning · Sep 07, 2018 at 09:48 AM ·
for-loopprefab-instance
Why is the color of my prefab instance not changing?
So i have this for loop and the random spawnpoints work just fine but the random color does not because every instance has the same color. Shouldnt the color change for each loop? What am i missing here..
void SpawnBooks()
{
for (int i = 1; i < bookCount; i++)
{
coverColor.color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 0.8f);
int spawnPointIndex = Random.Range(0, spawnPoints.Length);
Instantiate(bookPrefab, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
}
}
Comment
Best Answer
Answer by KittenSnipes · Sep 07, 2018 at 10:11 AM
@theLightning It is because you are only changing the color of a specified Renderer and not the newly created objects.
void SpawnBooks()
{
for (int i = 1; i < bookCount; i++)
{
int spawnPointIndex = Random.Range(0, spawnPoints.Length);
GameObject newBook = Instantiate(bookPrefab, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
newBook.GetComponent<Renderer>().material = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 0.8f);
}
}