I am trying to instantiate multiple circles sprites and then assigning each one a different color on it’s Sprite Renderer, however, what I’ve got ends up assigning them all the same color, specifically the last color assigned, like it continues to update all of the circle’s colors.
public GameObject circle;
public Transform spawnTransform;
public int numberOfCircles = 5;
public Color[] colors;
// Use this for initialization
void Start ()
{
GetAllColors ();
SpawnAllCircles ();
}
void GetAllColors()
{
colors [0] = Color.red;
colors [1] = new Color (255, 165, 0); //Orange
colors [2] = Color.green;
colors [3] = Color.blue;
colors [4] = Color.magenta;
}
void SpawnAllCircles()
{
for (int i = 0; i < numberOfCircles; i++)
{
GameObject newCircle = Instantiate (circle, spawnTransform);
newCircle.GetComponent<SpriteRenderer> ().sharedMaterial.SetColor ("_Color", colors *);*
-
}*
- }*
What I believe that should be happening is the for each of the five times the for loop is run, it’ll create the newCircle GameObject, then change the color. I’d assume the variable would be deallocated from memory after each instance of the for loop, so I don’t understand how this appears to be continuing to change the prior circle’s colors.