- Home /
Don't Understand Error
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
Answer by Berenger · Feb 17, 2014 at 01:05 PM
selectedAnswer is defined in the class Question. You need an instance of Question to access it from QuestionClass2.
You could try Unity's official tutorials, right here : http://unity3d.com/learn/tutorials/modules
Do i have to use GetComponent to access it from QuestionClass2
Answer by RudyTheDev · Feb 17, 2014 at 05:31 PM
There are a lot of issue with your code, but as for your specific error: you probably want to start with if (currentQuestion.selectedAnswer == correct)
and change your selectedAnswer
from private
to public
.
int selectedAnswer
is a variable in class Question
, and Question currentQuestion
is the instance of class Question
, so you access the variable of that instance with currentQuestion.selectedAnswer
.
now its saying it cant recognise correct. Ive said correctAnswerIndex = correct;
Well, yes, the variable correct
is not defined anywhere. As I said, a lot of code issues. You really need to begin by learning the basics of program$$anonymous$$g/C# first, we cannot write and debug all the code for you.
If i comment out the code that gives me errors, my code works. Can you explain why my code isnt very good, so i can learn how to fix it myself. Im not asking you to do it for me.
I'm sorry, but I cannot explain this to you the way you imagine. If you do not understand my answer, then it seems you have not learned to program yet. You are asking for someone to $$anonymous$$ch you to program, use C# and work in Unity. For that, there are hundreds of classes/books/tutorials/videos/samples out there that you can consult.
Your answer
![](https://koobas.hobune.stream/wayback/20220613133811im_/https://answers.unity.com/themes/thub/images/avi.jpg)