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 instead 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?
– iwaldrop