lundi 25 décembre 2017

Get different color from the current one every time, Unity

I have 4 colors. I want to make it so that the player can't be the same color 2 times in a row. When a player collides with an object, the RandomColor() is called. So this function is called many times during the game and sometimes the player does not change his color.

 using UnityEngine;

 public class ColorManager : MonoBehaviour {

     public SpriteRenderer player;
     public string playerColor;

     public Color[] colors = new Color[4];


     private void Awake()
     {
         RandomColor();        
     }

     public void RandomColor()
     {
         int index = Random.Range(0, 4);

         switch (index)
         {
             case 0:
                 player.color = colors[0]; //colors[0] is orange
                 playerColor = "orange";
                 break;

             case 1:
                 player.color = colors[1]; //colors[1] is pink
                 playerColor = "pink";
                 break;

             case 2:
                 player.color = colors[2]; //colors[2] is blue
                 playerColor = "blue";
                 break;

             case 3:
                 player.color = colors[3]; //colors[3] is purple
                 playerColor = "purple";
                 break;
             }    
     }    
 }

Tried using while loop, do while loop, but I'm obviously doing it wrong, since I receive the same color twice in a row sometimes. It would be great if anyone figures it out and explains how/why it works, because I spent a big chunk of time on the issue and I am very curious.




Aucun commentaire:

Enregistrer un commentaire