Я действительно новичок в кодировании, и у меня возникла первая проблема ... Через 4 часа после ее просмотра я понимаю, что, возможно, мне нужна помощь.
Код предназначен для 4 кнопок, каждая из которых имеет очень похожий код (на самом деле почти идентичный, за исключением переменной ID), однако 2 из них при загрузке говорят «аргументoutofrangeexception 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!");
}
}
}
}
Извиняюсь за действительно плохо отформатированный код, я не смог заставить его работать!