- Home /
The infamous "NullReferenceException: Object reference not set to an instance of an object" error
I have been getting this error for over 5 days now and still cannot fix it. what am I doing wrong? I have looked at almost every single forum with the same problem and have not been able to fix it. Would someone please take a look at my scripts and tell me what is going wrong?
The full error is " NullReferenceException: Object reference not set to an instance of an object ItemEffect.UseEffect () (at Assets/Inventory/Scripts/Items/ItemEffect.js:35)
Im trying to make it that when I pick up the steak in my inventory and click on it, it activates the function called EatSteak so that it heals my characters hunger by 10 points. But I get the NullReference error when I click on it. What am I missing here? can anyone help me please? I am at my wits end with this one simple error...
Thank you. Below are the two scripts I am using. The first script is called "ItemEffect" and is used for the Steak Item located in my inventory. When I click on the icon, it calls the UseEffect function, which is suppose to call the function from my second script...
#pragma strict
//This script allows you to insert code when the Item is used (clicked on in the inventory).
var deleteOnUse = true;
private var playersInv : Inventory;
private var item : Item;
var other : PlayerStatsV2;
@script AddComponentMenu ("Inventory/Items/Item Effect")
@script RequireComponent(Item)
//This is where we find the components we need
function Awake ()
{
other = gameObject.GetComponent(PlayerStatsV2);
playersInv = FindObjectOfType(Inventory); //finding the players inv.
if (playersInv == null)
{
Debug.LogWarning("No 'Inventory' found in game. The Item " + transform.name + " has been disabled for pickup (canGet = false).");
}
item = GetComponent(Item);
}
//This is called when the object should be used.
function UseEffect ()
{
other.EatSteak ();
//Play a sound
//playersInv.gameObject.SendMessage("PlayDropItemSound", SendMessageOptions.DontRequireReceiver);
//This will delete the item on use or remove 1 from the stack (if stackable).
if (deleteOnUse == true)
{
DeleteUsedItem();
}
}
//This takes care of deletion
function DeleteUsedItem()
{
if (item.stack == 1) //Remove item
{
playersInv.RemoveItem(this.gameObject.transform);
}
else //Remove from stack
{
item.stack -= 1;
}
Debug.Log(item.name + " has been deleted on use");
}
The second script is called "PlayerStatsV2" I'm trying to call the function EatSteak when I use the script from "ItemEffect", but it gives me the Object reference error.
#pragma strict
var CurrentHP : float = 100.0;
private var MaximumHP : int = 100.0;
private var CurrentStamina : float = 100.0;
private var MaximumStamina : float = 100.0;
// hunger for survival
public var CurrentHunger :float = 100.0;
private var MaximumHunger : int = 100;
// tempeture for survival
var CurrentTemp :float = 100.0;
private var MaximumTemp : int = 100;
//fallrates for survival
var HungerFallRate : int = 4;
var TempretureFallRate : int = 3;
function EatSteak ()
{
Debug.Log("this is working, why wont you heal!");
CurrentHunger += 10.00;
}
}
Thank you for taking the time to read this, and I hope someone can show me what it is I am doing wrong or missing.
Thank you again!
Well 'other' is null on line 35 of the first script. Are you sure the component, PlayerStatsV2, exists during awake?
There are one or two things you can try. Either make the 'other' veritable public so it can be assigned in the inspector (and assign i), or try doing an add component ins$$anonymous$$d of a get component (if there isn't anything that has to be configured in the inspector).
Also, can you put in to debug log after the get component in awake to check to see if 'other' is assigned?
Answer by fafase · Nov 30, 2013 at 09:05 AM
Try to move this line:
other = gameObject.GetComponent(PlayerStatsV2);
to the Start. It could be that this component is loaded before the PlayerStatsV2 and then does not find it. Moving it to the Start could fix it.
it is already on the function Awake, so it should be able to find it. I even created a function Start to make sure, but got the same result.
Nope, Components are loaded without specific order so it could be that your component looking for PlayerV2 is loaded but PlayerV2 is not yet done. Now if you want to make sure you can drag and drop the script in the variable from the Inspector.
im not sure I understand. do you mean loading it for the script? or the game? PlayerStats script contains my characters health and other functions, so it starts as soon as the game does. but the other script for using the item should only load up when I pick up the item. or am I misunderstanding you?
O$$anonymous$$! I found out what is going on. I assign the player to the Other variable, but when I start the game, it takes the player off and changes it back to none as the default. how could I fix this?
I recreated both as prefabs, but it still does it. I also heard of a bug that can make it happen if you have the same script on 2 separate game objects, but so far this is the only object with the item effect script on it.... $$anonymous$$
If you assign it manually then remove the line in the Awake.
Your answer
![](https://koobas.hobune.stream/wayback/20220613123110im_/https://answers.unity.com/themes/thub/images/avi.jpg)