繁体   English   中英

Argumentoutofrangeexception除外,不是吗? C#统一

[英]Argumentoutofrangeexception except it is not? C# unity

我对编码真的很陌生,遇到了第一个问题...看了四个小时之后,我意识到我可能需要一些帮助。

该代码是针对4个按钮的,所有按钮都有一个非常相似的代码(实际上,除了ID变量外,它们几乎相同),尽管其中的索引相同,但其中有2个在加载时会说“ argumentoutofrangeexception index”。 (或者我认为)

我在下面的代码中链接了一个不起作用的按钮和一个确实起作用的按钮+测试控件:

不工作按钮

public class Answer3script : MonoBehaviour {
    List<string> thirdchoice = new List<string>() { "first choice", "second choice", "third choice", "fourth", "fifth"};
    public static string answerCorrect3 = "n";

    void Update () {
        if (Textcontrol.randQuestion > -1)
        {
            GetComponentInChildren<Text>().text = thirdchoice[Textcontrol.randQuestion];
        }

        if (Textcontrol.correctAnswer[Textcontrol.randQuestion] == Textcontrol.buttonSelected)
        {
             answerCorrect3 = "y";
        }
    }
}

工作按钮

public class Answer2script : MonoBehaviour {
    List<string> secondchoice = new List<string>() { "first choice", "second choice", "third choice", "fourth choice", "fifth choice" };
    public static string answerCorrect2 = "n";

    void Update () {
        if (Textcontrol.randQuestion > -1)
        {
            GetComponentInChildren<Text>().text = secondchoice[Textcontrol.randQuestion];
        } 

        if (Textcontrol.correctAnswer[Textcontrol.randQuestion] == Textcontrol.buttonSelected)
        {
            answerCorrect2 = "y";

            //   if (answerCorrect2 == "y")
            //  {
            //      image.color = Color.green;
            //  }
        }
    }
}

文字控制:

public class Textcontrol : MonoBehaviour {
    List<string> questions = new List<string>() { "This is the first question", "second", "third", "fourth", "fifth" };
    public static List<string> correctAnswer = new List<string>() { "Answer1", "Answer2", "Answer3", "Answer4", "Answer4" };

     public static string buttonSelected;
     public static string choiceSelected = "n";
     public static int randQuestion=-1;

     void Update () {
         Image image  = GameObject.Find("Answer1").GetComponent<Image>();
         Image image2 = GameObject.Find("Answer2").GetComponent<Image>();
         Image image3 = GameObject.Find("Answer3").GetComponent<Image>();
         Image image4 = GameObject.Find("Answer4").GetComponent<Image>();

         if (randQuestion == -1)
         {
             randQuestion = Random.Range(0, 5);
         }

         if (randQuestion > -1)
         {
             GetComponent<Text>().text = questions[randQuestion];
         }

         if (choiceSelected == "y")
         {
             choiceSelected = "n";

             if (correctAnswer[randQuestion] == buttonSelected)
             {
                 Debug.Log("Correct!");
             }
         }
     }
}

对格式错误的代码表示歉意,我无法正常工作!

您这里有比赛条件! 可以在Textcontrol之一之前调用Answer3scriptUpdate ,因此randQuestion仍可以具有默认值-1

解决方案1

if可以同时在两个Update()进行操作, if可以代替第一个方法

private void Update()
{
    // Check if Textcontrol values are set already
    if (Textcontrol.randQuestion < 0 || Textcontrol.correctAnswer.Count < Textcontrol.randQuestion + 1 || Textcontrol.correctAnswer[Textcontrol.randQuestion] == null ) return;

    // ....
}

因此,如果Textcontrol尚未准备就绪,则什么也不会发生。

第二个条件|| Textcontrol.correctAnswer.Count < Textcontrol.randQuestion + 1 || Textcontrol.correctAnswer.Count < Textcontrol.randQuestion + 1确保列表中存在具有您要访问的索引的元素。

请注意, string值可以为null 因此,第三个条件|| Textcontrol.correctAnswer[Textcontrol.randQuestion] == null || Textcontrol.correctAnswer[Textcontrol.randQuestion] == null确保您访问的索引中的值实际上具有有效值。 如果还要避免使用空字符串( "" ),则可以将其扩展为|| string.IsNullOrEmpty(Textcontrol.correctAnswer[Textcontrol.randQuestion]) || string.IsNullOrEmpty(Textcontrol.correctAnswer[Textcontrol.randQuestion])


解决方案2

(我希望解决方案1)

转到Edit -> Project Settings -> Script Execution Order
在此处输入图片说明

单击+然后添加两个脚本Answer2ScriptAnswer3Script 您也可以简单地将它们拖动(一个一个地拖到字段上)。 如果不给Textcontrol一定的执行时间,它将在DefualtTime块中执行。 因此,只需确保您的两个脚本 DefaultTime 比点击Apply 这样可以确保两个脚本始终在“ Default Time -> Textcontrol之后”之后Textcontrol

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM