- Home /
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?
Answer by suribe · Mar 08, 2014 at 10:54 PM
You are initializing inventoryDictionary after using it it (line 97 onwards). You should initialize it first, probably in Start.
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.
I wouldn't know how to do it for the inventory slots. I'm not very good with loops.
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