我对编码真的很陌生,遇到了第一个问题...看了四个小时之后,我意识到我可能需要一些帮助。
该代码是针对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!");
}
}
}
}
为格式错误的代码表示歉意,我无法使它正常工作!
答案 0 :(得分:1)
您在这里有比赛条件! $className: heading;
.section .#{$className} {
color: pink;
&-sub {
color: red;
}
&-pre {
color: green;
}
}
的{{1}}可能在Update
之前被调用,因此Answer3script
仍可以具有默认值Textcontrol
在两个randQuestion
中,您都可以简单地代替
-1
因此,如果if
尚未准备就绪,则什么也不会发生。
第二个条件Update()
确保列表中存在具有您要访问的索引的元素。
请注意,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
确保您访问的索引处的值实际上具有有效值。如果还要避免使用空字符串(string
),则可以将其扩展为null
(我希望使用解决方案1)
转到|| Textcontrol.correctAnswer[Textcontrol.randQuestion] == null
-> ""
-> || string.IsNullOrEmpty(Textcontrol.correctAnswer[Textcontrol.randQuestion])
单击Edit
并添加两个脚本Project Settings
和Script Execution Order
。您也可以简单地将它们拖动(一个一个地拖到字段上)。在这里不给+
指定一定的执行时间,它将在DefualtTime块中执行。因此,只需确保您的两个脚本在Answer2Script
之后之后即可。比命中Answer3Script
。这样可以确保您的两个脚本始终在Textcontrol
之后的DefaultTime
->之后执行。