This is what I am working on: I am currently creating a Bingo game. I have made a list with all possible numbers and assign the letter based on the number chosen.
This is the issue that I am having: after maybe 15 numbers are chosen I get this error and the whole thing freezes up. I’m not sure how it’s trying to choose something beyond the scope of the list. It’s not a negative number so I know it’s not that.
Here is the code that I’ve come up with so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NumberGenerator : MonoBehaviour
{
#region Variables
[SerializeField] List<int> bingoNumberList;
[SerializeField] List<int> calledNumberList;
[SerializeField] Text calloutText;
int currentNumber;
char currentLetter;
#endregion
#region GameStart
private void Start()
{
for (int i = 1; i < 76; i++)
{
bingoNumberList.Add(i);
}
StartCoroutine(PlayBingo());
}
IEnumerator PlayBingo()
{
while (bingoNumberList.Count > 0)
{
NumberSelection();
yield return new WaitForSeconds(1f);
}
yield return null;
}
#endregion
#region Core
private void Update()
{
CalloutText();
}
private void NumberSelection()
{
if (bingoNumberList.Count > 0)
{
int randomIndex = bingoNumberList[Random.Range(0, bingoNumberList.Count)];
currentNumber = bingoNumberList[randomIndex];
LetterSelection();
Debug.Log(currentNumber);
Debug.Log(currentLetter);
calledNumberList.Add(currentNumber);
bingoNumberList.Remove(bingoNumberList[randomIndex]);
}
else
{
Debug.Log("All balls have been called!");
}
}
private void LetterSelection()
{
if (currentNumber >= 1 && currentNumber <= 15)
{
currentLetter = 'B';
}
else if (currentNumber >= 16 && currentNumber <= 30)
{
currentLetter = 'I';
}
else if (currentNumber >= 31 && currentNumber <= 45)
{
currentLetter = 'N';
}
else if (currentNumber >= 46 && currentNumber <= 60)
{
currentLetter = 'G';
}
else
{
currentLetter = 'O';
}
}
#endregion
#region CalloutText
public void CalloutText()
{
calloutText.text = "The current ball is " + currentLetter + currentNumber + "!";
}
#endregion
}