Making a game in unity and I am using this code. I don't know what is wrong and why doesn't the sprite changes color when instantiated. Can you help so I don't lose my mind? :D (I am spawning random GameObjects as well)
int randomIndex = UnityEngine.Random.Range(0, arrows.Length);
GameObject prefab = arrows[randomIndex];
GameObject clone = Instantiate(prefab, new Vector3(0.02F, 2.18F, -1), Quaternion.identity);
//change colors
colors[0] = new Color (250, 250, 250);
colors[1] = new Color (144, 249, 242);
colors[2] = new Color (20, 173, 163);
colors[3] = new Color (21, 129, 168);
colors[4] = new Color (5, 95, 127);
colors[5] = new Color (58, 125, 196);
int colorRandomIndex = UnityEngine.Random.Range(0, colors.Length);
SpriteRenderer renderer = clone.GetComponent<SpriteRenderer>();
renderer.color = colors[colorRandomIndex];
myObjects.Add(clone);
Maybe try renderer.material.color = colors[colorRandomIndex];
? It has been a while since I used Unity...
Could you try using
int colorRandomIndex = UnityEngine.Random.Range(0, colors.Length);
clone.GetComponent<SpriteRenderer>().material.color = colors[colorRandomIndex];
Okay so the error is the way you are creating your colors, Color only accepts float values between 0 and 1. You need to use Color32
colors[0] = new Color32 (250, 250, 250, 255);
colors[1] = new Color32 (144, 249, 242, 255);
colors[2] = new Color32 (20, 173, 163, 255);
colors[3] = new Color32 (21, 129, 168, 255);
colors[4] = new Color32 (5, 95, 127, 255);
colors[5] = new Color32 (58, 125, 196, 255);
Don't forget to also change the array to Color32
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.