Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Mint92 · Feb 17, 2014 at 12:55 PM · c#errorclass

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mint92 · Feb 17, 2014 at 01:15 PM 0
Share

any tutorials showing me how to do this, Big noob at code

avatar image Berenger · Feb 17, 2014 at 01:24 PM 0
Share

You could try Unity's official tutorials, right here : http://unity3d.com/learn/tutorials/modules

avatar image Mint92 · Feb 17, 2014 at 01:37 PM 0
Share

Do i have to use GetComponent to access it from QuestionClass2

avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mint92 · Feb 17, 2014 at 05:42 PM 0
Share

now its saying it cant recognise correct. Ive said correctAnswerIndex = correct;

avatar image RudyTheDev · Feb 17, 2014 at 06:46 PM 0
Share

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.

avatar image Mint92 · Feb 17, 2014 at 08:03 PM 0
Share

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.

avatar image RudyTheDev · Feb 17, 2014 at 09:05 PM 1
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

NullReferenceException yet Debug.Log shows nothing is null? 1 Answer

Object reference not set to an instance of an object in c# class. 1 Answer

The type or namespace name 'StateMachine' could not be found (are you missing a using directive or an assembly reference?) 1 Answer

"The class named is not derived from MonoBehaviour or ScriptableObject!", although class is derived from MonoBehaviour 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges