代码之家  ›  专栏  ›  技术社区  ›  Lechucico

ArgumentOutOfRangeException:参数超出范围[重复]

  •  0
  • Lechucico  · 技术社区  · 8 年前

    我有以下清单:

    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:参数超出范围。 参数名称:索引

    为什么会这样?

    编辑:我看到问题检查的索引总是15,为什么会这样?

    3 回复  |  直到 8 年前
        1
  •  2
  •   Lowkey    8 年前

    i 已经在 questionList.Count QuestionCheck 将与 问题列表。计数 (你的情况是15)

    我 改为使用:

    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 ));
    }
    
        2
  •  2
  •   CaTs    8 年前

    问题是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));
    }
    
        3
  •  0
  •   Technivorous    8 年前

    参数超出范围意味着在您的示例中,您的列表有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));
    }
    
    推荐文章