- Home /
Null Reference Exception - Object Not Set
I have followed JesseEtzler's Advanced Inventory Tutorial on YouTube. However I added my own touch to the script (which works fine) Just the way he referenced his objects is differen than how I need to do mine. My question is how can I achieve this? Thanks In advance.
Inventory Script Also is the EquipMenu script.
import System.Collections.Generic;
var items : Item[];
var MainInventory : List.<Item> = new List.<Item>();
var EquipMenu : Item[];
function Start () {
MainInventory.Add(items[0]);
MainInventory.Add(items[1]);
MainInventory.Add(items[2]);
MainInventory.Add(items[3]);
MainInventory.Add(items[4]);
}
function Update()
{
}
function OnGUI ()
{
for(var x = 0; x < MainInventory.Count; x++) {
if(GUI.Button(Rect(Screen.width/2 + 300, Screen.height/2 + 25 + (20 * x), 100, 20), "" + MainInventory[x].Name)) {
if(MainInventory[x].itemType == MainInventory[x].itemType.Cuirass ) {
if(MainInventory[x].itemFamily == MainInventory[x].itemFamily.Iron) {
if(MainInventory[x].itemID == MainInventory[x].itemID.IronCuirass ){
EquipMenu[0] = MainInventory[x];
MainInventory.RemoveAt(x);
EquipMenu[0].IsEquipped = true;
}
}
}
else if(MainInventory[x].itemType == MainInventory[x].itemType.Helm ) {
if(MainInventory[x].itemFamily == MainInventory[x].itemFamily.Iron) {
if(MainInventory[x].itemID == MainInventory[x].itemID.IronHelm ){
EquipMenu[1] = MainInventory[x];
MainInventory.RemoveAt(x);
EquipMenu[1].IsEquipped = true;
}
}
}
else if(MainInventory[x].itemType == MainInventory[x].itemType.Greaves ) {
if(MainInventory[x].itemFamily == MainInventory[x].itemFamily.Iron) {
if(MainInventory[x].itemID == MainInventory[x].itemID.IronGreaves ){
EquipMenu[2] = MainInventory[x];
MainInventory.RemoveAt(x);
EquipMenu[2].IsEquipped = true;
}
}
}
else if(MainInventory[x].itemType == MainInventory[x].itemType.Boots ) {
if(MainInventory[x].itemFamily == MainInventory[x].itemFamily.Iron) {
if(MainInventory[x].itemID == MainInventory[x].itemID.IronBoots ){
EquipMenu[3] = MainInventory[x];
MainInventory.RemoveAt(x);
EquipMenu[3].IsEquipped = true;
}
}
}
else if(MainInventory[x].itemType == MainInventory[x].itemType.Gauntlets ) {
if(MainInventory[x].itemFamily == MainInventory[x].itemFamily.Iron) {
if(MainInventory[x].itemID == MainInventory[x].itemID.IronGauntlets ){
EquipMenu[4] = MainInventory[x];
MainInventory.RemoveAt(x);
EquipMenu[4].IsEquipped = true;
}
}
}
}
}
for(var y = 0; y < EquipMenu.length; y++) {
if(EquipMenu[y] != null) {
if(GUI.Button(Rect(Screen.width/2 + 410, Screen.height/2 + 25 + (20 * y), 100, 20), "" + EquipMenu[y].Name)) {
MainInventory.Add(EquipMenu[y]);
EquipMenu[y].IsEquipped = false;
EquipMenu[y] = null;
}
}
}
}
Here is my Renderer Script that tells the Skinned Mesh Renderer when to be true or false This calls for a "true" or "false" statement from the Inventory and equipmenu
public var enabled : boolean;
renderer.enabled = false;
function Update() {
if(GameObject.Find("Human_M_Full1").GetComponent("Inventory").EquipMenu[0].IsEquipped) {
renderer.enabled = true;
}
else if(!GameObject.Find("Human_M_Full1").GetComponent("Inventory").EquipMenu.IsEquipped) {
renderer.enabled = false;
}
}
Here's the Exception Error I get* This happens for each piece of armor (mesh) I have *
NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetProperty (System.Object target, System.String name) UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name)
By the way this only happens when I Unequip the Item and try to reset my EquipMenu[y] back to null.
EquipMenu[y] = null;) that's when the error occurs.
Any help is greatly appreciated! Thanks guys!
It actually points to
if(GameObject.Find("Human_$$anonymous$$_Full1").GetComponent("Inventory").Equip$$anonymous$$enu[0].IsEquipped) {
renderer.enabled = true;
I didn't go through all the code but maybe if its not equiped you have to check first if: GameObject.Find("Human_$$anonymous$$Full1").GetComponent("Inventory").Equip$$anonymous$$enu[0] == null or GameObject.Find("Human$$anonymous$$_Full1").GetComponent("Inventory") == null and dont do anything in case it is.
Thanks for the the Reply Aridez, this is actually what I was wondering. Was how to implement a way to check to see if I could check the null status. I'm gonna try to add that I didn't know it had to be format "==" lol I'll feel so stupid if that's all I was doing wrong. :p
Oh man! Took a lot of toying around, but your idea worked! I got it all working properly man thank you so much!
Your answer
Follow this Question
Related Questions
Another Null Reference Exception 1 Answer
Help with eval() 0 Answers
NullReferenceException 1 Answer
NullReferenceException: Object reference not set to an instance of an object 3 Answers