- Home /
How can I spawn the same 2D sprite prefab with different colors randomly?
I want the prefabs to spawn with different colors from a spawnerObject that I have, but I don't want it to be completely random, I need it to intercalate between three colors like Red, Green and Blue. And I need to store the color in a variable so I can validate wich color the prefab spawned. In C#. I'm not sure if you can understand my question, but thanks anyways.
I know that i can use SpriteRenderer.color to change the color in the code, but my question is how can I interchange between those colors mentioned randomly. Just to make my question more understandable.
Answer by Trevdevs · Oct 16, 2018 at 03:13 AM
Your term "intercalcate" confuses I have no clue what you mean by that.
If I'm understanding you correctly what you want to do is randomly assign a sprite a color either Red, Blue or Green in this case. If this is true the use of a switch statement is what I recommend. Start by assigning each color to an integer, for example, Red is 0, Blue is 1, and Green is 2. Get an integer from a Random.Range(0, 3) include 3 because integers are exclusive. Then run it through a switch statement if the integer is 0 assign the spriterender.color to red and so on for the other colors.
Sorry, I really used the wrong term. But that's exactly what I wanted. Thanks a lot!
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
2D Game, Player Not to move if a wall is next to it C# 1 Answer
Add audio. 1 Answer