Is there a way for this random string generator to stop repeating a string consecutively?
public class ButtonText : MonoBehaviour
{
public Text TextField;
public void PickRandomFromList()
{
string[] letters = new string[] {"A", "B", "C", "D", "E", "F"};
string randomName = letters[Random.Range(0, letters.Length)];
TextField.text = randomName;
}
If you wish to avoid the function picking the same letter twice in a row, just store the previous value:
private string lastLetter="";
public void PickRandomFromList()
{
string[] letters = new string[] {"A", "B", "C", "D", "E", "F"};
string randomName;
do {
randomName = letters[Random.Range(0, letters.Length)];
} while (randomName == lastLetter);
lastLetter = randomName;
TextField.text = randomName;
}