Hey guys, I am really confused to why I'm getting this error
using UnityEngine;
using System.Collections;
public class QuestionClass2 : MonoBehaviour
{
public Question[] arrayOfQuestions;
public Question currentQuestion;
public bool showQuestion = true;
private int coinToss;
private bool heads;
private bool tails;
public class Question
{
private string questionText;
private Rect questionRect;
private string[] answers = new string[4];
private string[] displayAnswers = new string[4];
private Rect[] answerRects = new Rect[4];
private string correctAnswer;
private Rect correctRect;
private int correctAnswerIndex;
private int questionWidth = 300;
private int answerWidth = 75;
private int questionHeight = 1000;
private int correctWidth = 500;
private int selectedAnswer = -1;
public Question(Vector2 pos, string q, string a, string b, string c, string d, int correct)
{
questionText = q;
answers[0] = a;
answers[1] = b;
answers[2] = c;
answers[3] = d;
string st = "ABCD";
for(int i = 0; i < answers.Length; i++)
{
displayAnswers[i] = st[i] + ": " + answers[i];
}
correctAnswerIndex = correct;
correctAnswer = "Correct answer: " + answers[correct];
CalcRects (pos);
}
void CalcRects(Vector2 pos)
{
Rect rect = new Rect();
rect.x = pos.x;
rect.y = pos.y;
rect.width = questionWidth;
rect.height = questionHeight;
questionRect = rect;
rect.x += questionWidth;
rect.width = answerWidth;
for (int i = 0; i < answers.Length; i++)
{
answerRects[i] = rect;
rect.x += answerWidth;
}
rect.width = correctWidth;
correctRect = rect;
}
public void DisplayQuestion()
{
GUI.Label(questionRect, questionText);
for (int i = 0; i < answers.Length; i++)
{
GUI.Label(answerRects[i], displayAnswers[i]);
}
if (selectedAnswer != -1)
{
GUI.Label(correctRect, correctAnswer);
}
}
public void CheckForClick(Vector2 pos)
{
for (int i = 0; i < answers.Length; i++)
{
if (answerRects[i].Contains(pos))
{
selectedAnswer = i;
break;
}
}
}
}
void Start()
{
coinToss = Random.Range(0,2);
switch(coinToss)
{
case 0:
heads = true;
break;
case 1:
tails = true;
break;
}
Debug.Log(coinToss);
arrayOfQuestions = new Question [6];
arrayOfQuestions[5] = new Question(new Vector2(30,0), "heads or tails?","heads", "tails", "", "", coinToss);
//StartQuestions();
arrayOfQuestions = new Question [5]; // Initalizes an array of "Questions"
}
void StartQuestions()
{
arrayOfQuestions[0] = new Question(new Vector2(30,0), "How many league titles have Liverpool Won?", "18", "5", "10", "9",0);
arrayOfQuestions[1] = new Question(new Vector2(30,0), "Who won the World Cup in 2010?", "Germany", "Italy", "Brazil", "Spain", 3);
arrayOfQuestions[2] = new Question(new Vector2(30,0), "Who is the Manager of Manchester United?", "David Moyes", "Rafa Benitez", "Sir Alex Fergerson", "Brendan Rodgers", 0);
arrayOfQuestions[3] = new Question(new Vector2(30,0), "The Bundesliga is a professional association football league in which country?", "Italy", "Germany", "Norway", "Austria", 1);
arrayOfQuestions[4] = new Question(new Vector2(30,0), "Which footballer claimed that his hand-ball goal against England in 1986 was ‘The hand of God’?","Diego Maradona ", "Pele", "Wayne Rooney", "Gary Lineker", 0);
}
void ChooseRandomQuestion()
{
currentQuestion = arrayOfQuestions[Random.Range(0, arrayOfQuestions.Length)];
}
void Update()
{
if(selectedAnswer == correct)>>> Getting an error here.
{
ChooseRandomQuestion(); // Picks a random question
}
}
void OnGUI()
{
Event e = Event.current;
if (e.type == EventType.Repaint)
{
currentQuestion.DisplayQuestion(); //Only display the currentQuestion.
}
if (e.type == EventType.MouseDown)
{
foreach(Question thisQ in arrayOfQuestions)
{
thisQ.CheckForClick(e.mousePosition); // MINI: I didn't change this line, I'm guessing you dont need the for loop here, change it and see if it still works if you want.
}
}
}
}
Keep getting an error selectedAnswer does no exist in the current context. the only reason why i think im getting this error is because its in a class already. Not sure how to fix the problem
↧