• 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 /
This question was closed May 25, 2014 at 09:09 PM by tanoshimi for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by XXXX-XXXX · May 25, 2014 at 08:42 PM · c#nullreferenceexceptionobject-reference-not-set

An object reference that seems pretty set-to-an-instance-of-an-object to me

Here's all of the relevant and semi-relevant code:

     public string[] options;
     public string[] optionNames;
     
     public List<GameObject> menus;
     GameObject[][] subMenus;
 
     GameObject[,] menuText;
     int menu = 0;
     
     int holdCounter;
     float oneFrameAgo;
     GameObject cursor;
     int[] selected;
 
     void Awake () {
         menuText = new GameObject[100,100];
         MakeSubmenuOf (options, optionNames, this.gameObject, 0, 20, 0.5f, 0.5f, 0.7f);
         cursor = new GameObject ("Pause Cursor");
         cursor.AddComponent<GUIText>();
         cursor.transform.parent = this.gameObject.transform;
         this.gameObject.SetActive (false);
     }
     
     void Update () {
         cursor.guiText.color = Color.red;

 //-----------------------
         cursor.transform.position = menuText[menu,selected[menu]].transform.position + Vector3.forward * 0.02f;
 //-----------------------

         cursor.guiText.text = menuText[menu,selected[menu]].guiText.text;
         cursor.guiText.alignment = menuText[menu,selected[menu]].guiText.alignment;
         cursor.guiText.anchor = menuText[menu,selected[menu]].guiText.anchor;
         cursor.guiText.font = menuText[menu,selected[menu]].guiText.font;
         cursor.guiText.fontSize = menuText[menu,selected[menu]].guiText.fontSize;
     }
 
     public void MakeSubmenuOf(string[] menuToMake, string[] niceNames, GameObject parent, int number,
                               int fontSize, float x, float y, float offsetBetween) {
         GameObject subMenuToMake = parent;
         for (int i = 0; i < menuToMake.Length; i++) {
             menuText[number,i] = new GameObject(menuToMake[i]);
             menuText[number,i].transform.position = new Vector3(x,y - (i * offsetBetween), 0);
             menuText[number,i].transform.parent = subMenuToMake.transform;
             menuText[number,i].AddComponent<GUIText>();
             
             menuText[number,i].guiText.text = niceNames[i];
             menuText[number,i].guiText.alignment = TextAlignment.Center;
             menuText[number,i].guiText.anchor = TextAnchor.MiddleCenter;
             menuText[number,i].guiText.fontSize = fontSize;
         }
         subMenus [number] = new GameObject[menuToMake.Length];
     }

I marked the line at which I get an NRE. I don't know what to think.

Comment
Add comment · Show 2
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 Jeff-Kesselman · May 25, 2014 at 08:50 PM 1
Share

There js LOT of dereferncing going on in that line check ALL of the following individually for non null values:

 cursor
 menuText 
 selected
 menu
 selected[menu]
 menuText[menu,selected[menu]]
avatar image XXXX-XXXX · May 25, 2014 at 08:59 PM 0
Share

selected was it. Thanks again.

0 Replies

  • Sort: 

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

22 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 avatar image avatar image

Related Questions

Nested Class Initialization 1 Answer

NullReferenceException help 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Distribute terrain in zones 3 Answers

C# nullreferenceexception object reference not set 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges