ArgumentOutOfRangeException:参数超出范围

时间:2018-10-11 22:31:18

标签: c# unity3d exception outofrangeexception

我有以下列表:

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;
}

我在开始时初始化以下按钮:

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));
}

我有以下代码:

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();
    }
}

我认为它在以下行崩溃:

if (currentQuestion.answer == questionList[index].answer)

如果我尝试以下行,它也会崩溃:

questionList.RemoveAt(index);

我收到以下错误:

  

ArgumentOutOfRangeException:参数超出范围。   参数名称:索引

为什么会这样?

编辑:我已经看到QuestionCheck的索引始终为15,为什么会这样呢?

3 个答案:

答案 0 :(得分:2)

这是范围问题。事件一旦发生,i已经在questionList.Count了,它不在数组范围内。您的所有QuestionCheck都将被questionList.Count调用(在您的情况下为15)

您要做的是将i保存为一个临时值,并使用该值:

for (int i = 0; i < questionList.Count; i++)
{
    var currentIndex = i;
    GameObject child = Instantiate(questionButton, buttonLayout.transform);
    child.GetComponent<Text>().text = questionList[i].answer;
    child.GetComponent<Button>().onClick.AddListener(() => QuestionCheck(currentIndex ));
}

答案 1 :(得分:2)

问题是您的onClick侦听器不在循环内评估,而是在单击按钮后进行评估。

child.GetComponent<Button>().onClick.AddListener(() => QuestionCheck(i));

所以当循环运行时我等于0,1,3 ... 14时,当您最终单击按钮时,循环已经完成很长时间了,我等于15。

解决此问题的一个简单方法是创建另一个变量,该变量将保存i的当前值。

for (int i = 0; i < questionList.Count; i++)
{
    int questionIndex = i;
    GameObject child = Instantiate(questionButton, buttonLayout.transform);
    child.GetComponent<Text>().text = questionList[questionIndex].answer;
    child.GetComponent<Button>().onClick.AddListener(() => QuestionCheck(questionIndex));
}

答案 2 :(得分:0)

参数超出范围意味着在您的情况下,您的列表包含3个项目,并且它试图访问4、5或比列表中的range高的任何数字。

还请记住,您的列表从0开始,所以您的项分别是0、1和2

代替

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));
}

尝试

foreach(GameManager.Question q in questions){
    GameObject child = Instantiate(questionButton, buttonLayout.transform);
        child.GetComponent<Text>().text = q.answer;
        child.GetComponent<Button>().onClick.AddListener(() => QuestionCheck(i));
}