I have the following List:
public Question init()
{
questions = new List<GameManager.Question>();
questions.Add(new GameManager.Question("Ya no hay técnicas que te puedan salvar.", "Sí que las hay, sólo que nunca las has aprendido."));
questions.Add(new GameManager.Question("Espero que tengas un barco para una rápida huida.", "¿Por qué? ¿Acaso querías pedir uno prestado?"));
questions.Add(new GameManager.Question("Ahora entiendo lo que significan basura y estupidez.", "Me alegra que asistieras a tu reunión familiar diaria."));
return questions;
}
I initialize the following buttons on start:
for (int i = 0; i < questionList.Count; i++)
{
GameObject child = Instantiate(questionButton, buttonLayout.transform);
child.GetComponent<Text>().text = questionList[i].answer;
child.GetComponent<Button>().onClick.AddListener(() => QuestionCheck(i));
}
And I have the following code:
public void QuestionCheck(int index)
{
if (currentQuestion.answer == questionList[index].answer)
{
playerAsk = 1;
playerScore += 1;
scorePlayer.GetComponent<Text>().text = "Jugador: " + playerScore;
roundStatus.text = "Has ganado la ronda!";
roundStatus.color = Color.green;
correct.Play();
}
}
I think it crashes on the following line:
if (currentQuestion.answer == questionList[index].answer)
Also if I try the following line it also crashes:
questionList.RemoveAt(index);
I obtain the following error:
ArgumentOutOfRangeException: Argument is out of range. Parameter name: index
Why this is happening?
EDIT: I've seen that the index from QuestionCheck is always 15, why this is happening?