Need help with a Null Reference Exception Error
I have run into a very weird Null Reference Exception Error. Pretty much whats going on is that I have a menu for a turn based battle system I'm working on, and am currently working on being able to go back if you selected the wrong move. This system works very well for me however it only work correctly if I have the inspector open and focused on the script that is being used to run all this. If the inspector in on a different object then what the script is attached too, or just isn't open at all I run into a Null Reference Exception. This just seems super bizarre to me and I'm still pretty new too unity so I have no idea what to do.
Also let me know if you need more information and I'll do my best to provide it.
Of course, here is the full error.
NullReferenceException: Object reference not set to an instance of an object Battle_Event.GoBack () (at Assets/Scripts/Battle_Event.cs:589) Battle_Event.Update () (at Assets/Scripts/Battle_Event.cs:245)
Additionally here is the code that it is referring to, line 589 is the first if statement. After a bit more missing around I noticed that this only happens if I'm trying to make the 3rd if statement be true. It also doesn't matter what order the if statements are arranged in, it will only return the error if the first 2 are suppose to be false.
case BattleSequence.selectingPlayer:
if (playerTurnStorage[PTSNum].selectedSkill.defence > 0)
{
//Gets rid of the PTS
playerTurnStorage.Remove(playerTurnStorage[PTSNum]);
currentSequence = BattleSequence.selecting$$anonymous$$ove;
//Sets the correct cursor to be active
UI_Cursor.S.gCursor.SetActive(true);
UI_Cursor_1.S.gCursor.SetActive(false);
break;
}
if (playerTurnStorage[PTSNum].selectedSkill.name != null && playerTurnStorage[PTSNum].selectedSkill.defence == 0)
{
//Gets rid of the PTS
playerTurnStorage.Remove(playerTurnStorage[PTSNum]);
//$$anonymous$$akes the Skills slide back down
Battle_Skill_$$anonymous$$enu.S.slideDownSkills = true;
currentSequence = BattleSequence.selectingSkill;
//Sets the correct cursor to be active
UI_Cursor.S.gCursor.SetActive(true);
UI_Cursor_1.S.gCursor.SetActive(false);
break;
}
if (playerTurnStorage[PTSNum].selectedItem.name != null)
{
//Gets rid of the PTS
playerTurnStorage.Remove(playerTurnStorage[PTSNum]);
//$$anonymous$$akes the Items slide back down
Battle_Item_$$anonymous$$enu.S.slideDownItems = true;
currentSequence = BattleSequence.selectingItem;
//Sets the cursor to be false
UI_Cursor_1.S.gCursor.SetActive(false);
break;
}
break;
Alright well I manged to figure out a work around, however I'm still curious why the error showed up when I didn't have the Inspector pulled up. I mean why would that effect whether playerTurnStorage[PTSNum].selectedItem is null or not.