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
1
Question by NutellaDaddy · Mar 08, 2014 at 10:44 PM · c#guidictionary

When I try to make a dictionary I get a Null Reference Exception! Why?

When I define the dictionary and then put it into the GUI Buttons, which I'm using for inventory Slots, it says the exact error right here: Object reference not set to an instance of an object inventorySystem.OnGUI () (at Assets/Scripts/inventorySystem.cs:56)

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class inventorySystem : MonoBehaviour 
 {
     //PUBLIC VARIABLES
     public bool inventoryOn = false;
     public Texture inventoryWindow;
 
     //GUI FONT,SIZE, STYLE, AND LAYOUT
     public Vector2 windowPosition = new Vector2(0,0);
     public Vector2 windowSize = new Vector2(800,800);
 
     public GUIStyle inventorySlotStyle;
     public GUIStyle inventoryLabelStyle;
 
     //MAX WEIGHT
     public int weiLimit = 150;
     public int curWei = 0;
 
     //DICTIONARY
     private Dictionary<int,string> inventoryDictionary;
 
 
     void Start () 
     {
     
 
     }
     void Update () 
     {
         //INVENTORY ON AND OFF
         if (Input.GetKeyUp (KeyCode.Tab))
         {
             if (inventoryOn == false) 
             {
                 inventoryOn = true;
             } 
             else if (inventoryOn == true) 
             {
                 inventoryOn = false;
             }
         }
     }
 
     void OnGUI()
     {
         //INVENTORY
         if (inventoryOn == true) 
         {
             GUI.BeginGroup (new Rect(windowPosition.x,windowPosition.y,windowSize.x,windowSize.y),inventoryWindow);
             //INVENTORY SLOTS
 
             //ROW ONE
             GUI.Button (new Rect(291,64,78,78),inventoryDictionary[0], inventorySlotStyle);
             GUI.Button (new Rect(379,64,78,78),inventoryDictionary[1], inventorySlotStyle);
             GUI.Button (new Rect(467,64,78,78),inventoryDictionary[2], inventorySlotStyle);
             GUI.Button (new Rect(555,64,78,78),inventoryDictionary[3], inventorySlotStyle);
             GUI.Button (new Rect(643,64,78,78),inventoryDictionary[4], inventorySlotStyle);
             GUI.Button (new Rect(731,64,78,78),inventoryDictionary[5], inventorySlotStyle);
 
             //ROW TWO
             GUI.Button (new Rect(291,152,78,78),inventoryDictionary[6], inventorySlotStyle);
             GUI.Button (new Rect(379,152,78,78),inventoryDictionary[7], inventorySlotStyle);
             GUI.Button (new Rect(467,152,78,78),inventoryDictionary[8], inventorySlotStyle);
             GUI.Button (new Rect(555,152,78,78),inventoryDictionary[9], inventorySlotStyle);
             GUI.Button (new Rect(643,152,78,78),inventoryDictionary[10], inventorySlotStyle);
             GUI.Button (new Rect(731,152,78,78),inventoryDictionary[11], inventorySlotStyle);
 
             //ROW FOUR
             GUI.Button (new Rect(291,240,78,78),inventoryDictionary[12], inventorySlotStyle);
             GUI.Button (new Rect(379,240,78,78),inventoryDictionary[13], inventorySlotStyle);
             GUI.Button (new Rect(467,240,78,78),inventoryDictionary[14], inventorySlotStyle);
             GUI.Button (new Rect(555,240,78,78),inventoryDictionary[15], inventorySlotStyle);
             GUI.Button (new Rect(643,240,78,78),inventoryDictionary[16], inventorySlotStyle);
             GUI.Button (new Rect(731,240,78,78),inventoryDictionary[17], inventorySlotStyle);
 
             //ROW FIVE
             GUI.Button (new Rect(291,328,78,78),inventoryDictionary[18], inventorySlotStyle);
             GUI.Button (new Rect(379,328,78,78),inventoryDictionary[19], inventorySlotStyle);
             GUI.Button (new Rect(467,328,78,78),inventoryDictionary[20], inventorySlotStyle);
             GUI.Button (new Rect(555,328,78,78),inventoryDictionary[21], inventorySlotStyle);
             GUI.Button (new Rect(643,328,78,78),inventoryDictionary[22], inventorySlotStyle);
             GUI.Button (new Rect(731,328,78,78),inventoryDictionary[23], inventorySlotStyle);
 
             //EQUIP SLOTS
 
 
             //EXTRAS
             GUI.Label(new Rect(731,407,78, 25), "Weight: " + curWei + " / " + weiLimit, inventoryLabelStyle);
 
             GUI.EndGroup ();
             //QUICK SELECTION BAR
 
             //DICTIONARY
             inventoryDictionary = new Dictionary<int, string>()
             {
 
                 {0, string.Empty},
                 {1, string.Empty},
                 {2, string.Empty},
                 {3, string.Empty},
                 {4, string.Empty},
                 {5, string.Empty},
                 {6, string.Empty},
                 {7, string.Empty},
                 {8, string.Empty},
                 {9, string.Empty},
                 {10, string.Empty},
                 {11, string.Empty},
                 {12, string.Empty},
                 {13, string.Empty},
                 {14, string.Empty},
                 {15, string.Empty},
                 {16, string.Empty},
                 {17, string.Empty},
                 {18, string.Empty},
                 {19, string.Empty},
                 {20, string.Empty},
                 {21, string.Empty},
                 {22, string.Empty},
                 {23, string.Empty},
                 {24, string.Empty},
                 {25, string.Empty},
                 {26, string.Empty},
                 {27, string.Empty},
                 {28, string.Empty},
                 {29, string.Empty}
 
             };
 
         }
     }
 }
 

It is also passing an error saying that the references script on this behavior is missing. Any Help please?

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

1 Reply

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

Answer by suribe · Mar 08, 2014 at 10:54 PM

  1. You are initializing inventoryDictionary after using it it (line 97 onwards). You should initialize it first, probably in Start.

  2. Are you setting the value of the variable inventorySlotStyle from the editor? If not, then it will be null also.

BTW, there are some things you could improve in the code. For example, use this for intializing the dictionary instead of writting 30 lines:

 inventoryDictionary = new Dictionary<int, string>();
 for (int i = 0 ; i < 30 ; i++) {
     inventoryDictionary.Add(0, string.Empty);
 }

And also something similar with the display in the GUI.

Comment
Add comment · Show 2 · 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 NutellaDaddy · Mar 09, 2014 at 04:07 AM 0
Share

I wouldn't know how to do it for the inventory slots. I'm not very good with loops.

avatar image suribe · Mar 09, 2014 at 08:35 AM 0
Share

Well, I'll show you one way: You are adding a row every 6 items, and each takes 88 pixels. Let's do some math with divisions, forcing integers and re$$anonymous$$ders:

  • i = 0 (int)(i / 6) = 0, rem(i/6) = 0

  • i = 1 (int)(i / 6) = 0, rem(i/6) = 1

  • i = 2 (int)(i / 6) = 0, rem(i/6) = 2

  • i = 3 (int)(i / 6) = 0, rem(i/6) = 3

  • i = 4 (int)(i / 6) = 0, rem(i/6) = 4

  • i = 5 (int)(i / 6) = 0, rem(i/6) = 5

  • i = 6 (int)(i / 6) = 1, rem(i/6) = 0

  • i = 7 (int)(i / 6) = 1, rem(i/6) = 1

... etc.

So lets create two helper functions:

 int Col(int i) {
   return 291 + (i % 6) * 88;
 }
 
 int Row(int i) {
   return 64 + ((int)(i / 6)) * 88;
 }
 

So:

  • i = 0 -> Col(i) = 291, Row(i) = 64

  • i = 1 -> Col(i) = 379, Row(i) = 64

  • i = 2 -> Col(i) = 467, Row(i) = 64

  • i = 3 -> Col(i) = 555, Row(i) = 64

  • i = 4 -> Col(i) = 643, Row(i) = 64

  • i = 5 -> Col(i) = 731, Row(i) = 64

  • i = 6 -> Col(i) = 291, Row(i) = 152

  • i = 7 -> Col(i) = 379, Row(i) = 152

with that in $$anonymous$$d, you create a loop like this:

 for (int i = 0 ; i < 30 ; i++) {
          GUI.Button (new Rect(Col(i),Row(i),78,78), inventoryDictionary[i], inventorySlotStyle);
 }
 

$$anonymous$$aybe there are a couple syntax errors, I just woke up and wrote this without checking it with the compiler. But it should give you a good idea of how to solve it. :)

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

21 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

Related Questions

error CS1526: A new expression requires () or [] after type 1 Answer

Why does my GUI health bar move sideways when it I lose health? 1 Answer

Having trouble moving item from one list to another! 1 Answer

Creating Q&A Quiz game using Dictionaries as the database 1 Answer

Getting almost nowhere with my buttondrag script 1 Answer


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