- Home /
It's only a basic problem of forgetting.
Null Reference Exception
Hello, I have a problem. I'am making a RPG game so i need inventory, all work for the moment, except one thing. I create a function to equip an item :
public void EquipItem()
{
if (itemObjects[itemObjects.Count - 1].item.Type != Item._Type.Consumable && itemObjects[itemObjects.Count - 1].item.Type != Item._Type.Quest)
{
equipment.EquipItem(itemObjects[itemObjects.Count - 1]);
}
}
This function use a list (for itemObjets), the conditions work, but not the result :(equipment.EquipItem(itemObjects[itemObjects.Count - 1]);)
The console show this error :
(NB: the line 74 is : equipment.EquipItem(itemObjects[itemObjects.Count - 1]);
) in the script
For me it's impossible because, the conditions work with the "index" of list : itemObjects.Count - 1
, but why the "index" of list don't work on line 74 equipment.EquipItem(itemObjects[itemObjects.Count - 1]);
I need your help please!
Sorry for my bad English i'm French ^^
Thanks in advance.
Try to put print(itemObjects.Count)
before the if statement and have a look at the count.
Thank you for your ansew! :)
I did it, and the console show 1 So with the formula itemObjects.Count - 1
we get 0, therefore the first itemObject in the list itemObjects.
It should work, don't understand the problem. :'(
PS: the formula itemObjects.Count - 1
is for select the last itemObject in list.
Plain debugging questions, especially about null reference exceptions are classified as too basic and off topic:
Posts that include a compiler error or NullReferenceException: all of these compiler errors (such as BCE0001) are included as tags, and there are multiple questions already. These new posts need to be tagged and then rejected.
A NullReferenceException is one of the most basic concepts of C# / .NET and nothing Unity specific. Apart from that your question does not contain enough information. A null ref exception is always the same thing. You have a variable that contains a null value and you try to access the object that is see$$anonymous$$gly behind that reference. In your code that means either the element in your "itemObjects" array is null, or your "equipment" variable is null, or the issue is actually inside your "EquipItem" method. We don't see your stacktrace so we can't say anything else about that.
Debugging is a major part of program$$anonymous$$g and you should be able to solve such basic issues yourself. The stacktrace of the error tells you the exact line. If you have too many statements in that single line you should seperate things into multiple lines.
Indeed, you are a thousand times right, in normal times I am able to fix these basic types of problems, the error was just to 'equipment' which the variable that is automatically assigned by a 'FindObjectOfType<>()' but I completely forgot to assign the script equipment component to a GameObject, thanks for your help, I will be careful next time and I will avoid bothering you with such basic problems.
I hope I've been able to make myself understood. Goodbye :)