What can I do to this Random.Range so it doesn't generate previous number every time?

Hi all I am making a game from Color Switch Template. I am making it into another game. Here the problem is that every time ball collides with any object this function is called.

/// <summary>
/// This Method changes the color of the player, and the active colliders in the scene.
/// </summary>
public void SwitchPlayerColor()
{
    UnityEngine.Random.InitState((int)DateTime.Now.Ticks);
    playerColor =(PlayerColor)UnityEngine.Random.Range(0, 3);
    //we create an int and give it the same value as the int casted Enum state (ie 0,1,2,3,pink,purple,teal,yellow)

    //when the playersColor enum is a 0 or 1 or 2 or 3 these case statements are executed and the following occurs
    switch (playerColor)
    {
        case PlayerColor.Pink:
            playerRenderer.material.color = playerColorArray[0];

            break;
        case PlayerColor.Purple:
            playerRenderer.material.color = playerColorArray[1];

            break;
        case PlayerColor.Teal:
            playerRenderer.material.color = playerColorArray[2];

            break;
        case PlayerColor.Yellow:
            playerRenderer.material.color = playerColorArray[3];

            break;
        default:
            Debug.Break();
            Debug.Log("Color is Broken");

            break;
    }
    pAnimator.SetTrigger("Jump");
    chgscr.IncreaseScore();

}  

The problem is that when this function is called the random color generated is same as previous number. What can I do so the random number generated isn’t same as previous number ??

public static int prevColor = 100;

playerColor =(PlayerColor)UnityEngine.Random.Range(0, 3);

while(prevColor == playerColor)

playerColor =(PlayerColor)UnityEngine.Random.Range(0, 3);

prevColor = playerColor;

All you need is
one static variable.

Thank You Very Much. It worked. :slight_smile: