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 19, 2014 at 12:30 PM · c#classesonguiobject-reference-not-set

I'm confused

I have two classes within a script using UnityEngine; using System.Collections;

 public class QuestionClass2 : MonoBehaviour 
 {
     public Question[] arrayOfQuestions;
     public Question currentQuestion;
     public bool showQuestion = true;
     private int coinToss;
     public bool heads;
     public bool tails;
     //public QuestionClass2 test;
     
     public class Question
     {
         
        public int selectedAnswer = -1;
        public int correctAnswerIndex;
        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 questionWidth = 300;
        private int answerWidth = 75;
        private int questionHeight = 1000;
        private int correctWidth = 100;
        
  
        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 OnGUI() { Event e = Event.current;

    if (e.type == EventType.Repaint)
        {
             //QuestionClass2 script = (QuestionClass2)(GameObject.Find("QuestionManager").GetComponent("QuestionClass2"));
             currentQuestion.DisplayQuestion();
        }
 
    //if (e.type == EventType.MouseDown)
   // {
          //foreach(Question thisQ in arrayOfQuestions)
         // {
          // thisQ.CheckForClick(e.mousePosition); 
          //}
   // }
 }

}

Ive clearly stated that currentQuestion is appart of QuestionClass2 and DisplayQuestion is apart of the QuestionClass, why am i getting an error object reference not set to an instance of object.

Comment
Add comment · Show 1
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 19, 2014 at 09:49 AM 0
Share

getting error in the Void onGui function

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by RudyTheDev · Feb 19, 2014 at 01:40 PM

That's because you haven't initialized/created the currentQuestion, you only specified its type is Question. That is not enough. Your game doesn't know what the currentQuestion holds inside, because you haven't called the constructor (`public Question(...)`), so it is null. This is what your error says -- "object reference not set to an instance of object", in other words, the variable currentQuestion that is supposed to hold an object reference to some Question doesn't have one.

You have to, at least for starters, do

 currentQuestion = new Question(new Vector2(30, 0), "which one?", "one", "two", "three", "four", 2);

Probably in void Start() until you add proper selection. Your old code had void Start() with question initialization, but it seems you have deleted that from this version.

Comment
Add comment · Show 1 · 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 19, 2014 at 04:58 PM 0
Share

thanks man Ive fixed it and rearranged the whole script now

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

19 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Make custom editor for generic class to apply to all current and future child classes 1 Answer

How to remove GUILayer? 2 Answers

Classes and Scripting 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