
It looks like you're new here. If you want to get involved, click one of these buttons!
Hello, today I tried to make a healing potion for my first game. I've made 2 methods, UpdateUI() and HealPlayer(). When you click on the healing potion in the inventory, a button component calls the "HealPlayer()"-function. Everything works fine, but when I click on the potion, I get an error "Object Reference not set to an instance of an object ". I've seen many people having this problem, but no solution worked for my problem. Can someone help me? Thank you.
Comments
Usually that error is received when not all variables in the inspector have been filled. Do you have 'potion' variable or something like that that you just need to put a reference into?
As @Etonio99 said, there is probably a reference that you are missing. As I see, "HealthText" is public, so you would want to drag and drop the specific object from the Hierarchy to the desired reference in Inspector.
Did you say
using UnityEngine.UI;
;on the top of the page?An Object is an instance of a Class , it is stored some where in memory. A reference is what is used to describe the pointer to the memory location where the Object resides. The message "object reference not set to an instance of an object" (NullReferenceException) means that you are referring to an object the does not exist or was deleted or cleaned up. In order to prevent the error, objects that could be null should be tested for null before being used.
if (mClass != null)
{
// Go ahead and use mClass
mClass.property = ...
}
else
{
// Attempting to use mClass here will result in NullReferenceException
}
A NullReferenceException typically reflects developer error and is thrown in the following scenarios: