I have 2 scripts. Questionnaire and QuestionList. The questionnaire script contains the gameplay and questionlist is the well list of course. I got it all to work fine in 1 single script. But as you can imagine that would take forever to scroll through thousands of questions to get to the code for debuging so I decided to move them to there own script QuestionList. I need help accessing the list on QuestionList from the Questionnaire script. Its basicly grabing questions from the list and displaying them. Please let me know what I’m doing wrong here.
Script 1
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Questionnaire : MonoBehaviour {
IEnumerator Wait()
{
GUILayout.Label("Correct!");
yield return new WaitForSeconds(2.5f);
//....
}
public int index;
class Question
{
public string QuestionText;
public List<string> Answers = new List<string>();
public int CorrectAnswer;
}
void Awake()
{
//Get a question at runtime.
GetQuestion();
}
Question GetQuestion()
{
List<Question> aList = new List<Question>();
string question = this.gameObject.GetComponent<QuestionList>().list[index].QuestionText;
int rand = Random.Range(0, aList.Count);
index = rand;
return aList[rand];
}
void OnGUI()
{
//--------------------------For Debug Purposes--------------------------------
GUI.Label(new Rect(10, 10, 100, 20), "Questions: " + aList.Count);
GUI.Label(new Rect(10, 30, 200, 20), "Current Question: " + index.ToString());
GUI.Label(new Rect(10, 60, 200, 20), "Current Answers: " + aList[index].Answers);
//-----------------------------------------------------------------------------
//Create the GUILayout area.
GUILayout.BeginArea(new Rect((Screen.width - 200) / 2, (Screen.height - 200) / 2, 200, 200));
//Display a question.
GUILayout.BeginVertical();
GUILayout.Label(aList[index].QuestionText);
//Display the answers.
GUILayout.BeginHorizontal();
for (int i = 0; i <= 3; i++ )
{
if (GUILayout.Button(aList[index].Answers*))*
{
//If correct answer…
if (i == aList[index].CorrectAnswer)
{
Debug.Log(“Correct”);
if (index < aList.Count)
{
aList.Remove(aList[index]);
GetQuestion();
} //Here we would add the Ending…
else { Debug.LogError(“Out Of Questions To Display”); }
}
else //if incorrect…
{
Debug.Log(“Incorrect”);
GetQuestion();
}
}
}
GUILayout.EndHorizontal();
GUILayout.Space(10);
GUILayout.EndVertical();
GUILayout.EndArea();
}
}
And script 2…
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class QuestionList : MonoBehaviour {
public class Question
{
public string QuestionText;
public List Answers = new List();
public int CorrectAnswer;
}
public List list = new List();
void Awake()
{
//Questions and answers are listed here. Index beginning at 0.
//The CorrectAnswer property should set to the correct
//Index of the correct answer in this first one you can see
//Correct answer is index “3”. 0, 1, 2, and 3(“Four Text”).
list.Add(new Question());
list[0].QuestionText = “What is 2 + 2?”;
list[0].Answers.Add(“1”);
list[0].Answers.Add(“2”);
list[0].Answers.Add(“22”);
list[0].Answers.Add(“4”);
list[0].CorrectAnswer = 3;
list.Add(new Question());
list[1].QuestionText = “What is 4 + 4?”;
list[1].Answers.Add(“4”);
list[1].Answers.Add(“44”);
list[1].Answers.Add(“8”);
list[1].Answers.Add(“100”);
list[1].CorrectAnswer = 2;
list.Add(new Question());
list[2].QuestionText = “What is 6 + 6?”;
list[2].Answers.Add(“16”);
list[2].Answers.Add(“9”);
list[2].Answers.Add(“12”);
list[2].Answers.Add(“6,000,000”);
list[2].CorrectAnswer = 2;
list.Add(new Question());
list[3].QuestionText = “What is 8 + 8?”;
list[3].Answers.Add(“2”);
list[3].Answers.Add(“88”);
list[3].Answers.Add(“5”);
list[3].Answers.Add(“16”);
list[3].CorrectAnswer = 3;
}
}