- Home /
NullReferenceException: Object reference not set to an instance of an object
Hello I am trying to make a game and keep getting this error message:
NullReferenceException: Object reference not set to an instance of an object
GUI_HUD.OnGUI () (at Assets/Scripts_/GUI_HUD.js:23).
And I for one have no idea what it means and my friend and I have trying to figure it out. The code of the GUI_HUD looks like this
var customSkin:GUISkin;
var EXPImage:Texture2D;
var healthpotionImage:Texture2D;
var manapotionImage:Texture2D;
private var customControls:GUI_CustomControls;
private var playerInvo:Player_Inventory;
function Awake()
{
customControls = FindObjectOfType(GUI_CustomControls);
playerInvo = FindObjectOfType(Player_Inventory);
}
function OnGUI()
{
if(customSkin)
{
GUI.skin = customSkin;
}
//Inventory Buttons --------------------------------------
if(customControls.InvoHudButton(Rect(10, Screen.height - 100, 93, 95), playerInvo.GetItemCount(InventoryItem.MANAPOTION), manapotionImage, "Click To Use a Mana Potion"))
{
playerInvo.UseItem(InventoryItem.MANAPOTION, 1);
}
if(customControls.InvoHudButton(Rect(110, Screen.height - 100, 93, 95), playerInvo.GetItemCount(InventoryItem.HEALTHPOTION),healthpotionImage, "Click To Use a Health Potion"))
{
playerInvo.UseItem(InventoryItem.HEALTHPOTION, 1);
}
//Non Usable Inventory Buttona ------------------------------
customControls.InvoHudButton(Rect(Screen.width - 210, Screen.height - 100, 93, 95), playerInvo.GetItemCount(InventoryItem.EXP), EXPImage, "Amount of EXP");
}
function Start () {
}
the GUI_CustomControls code looks like this
function InvoHudButton(screenPos: Rect, numAvailable : int, itemImage: Texture, itemtooltip: String ) : boolean
{
if( GUI.Button(screenPos, GUIContent(itemImage, itemtooltip), "HUD Button") )
{
return true;
}
GUI.Label( Rect(screenPos.xMax - 20, screenPos.yMax - 25, 20, 20 ), numAvailable.ToString() );
//display area for tooltips
GUI.Label( Rect( 20, Screen.height - 130, 500, 100), GUI.tooltip);
}
Answer by MountDoomTeam · Oct 13, 2012 at 03:21 AM
have a look, the error is online 23, it says in the error message. It means that the command you wrote on line 23 is calling an object/component, except that you haven't specified properly the componentthat it supposed to call, the variable is empty basically something to that effect. so you have to figure out a way to specify the variable that it is calling so is properly initialised to a game objects/components/it has found something to execute on.
please tell us exactly what is written on line 23 because it's a bit finicky to count without the line numbers. likethat we can see the line in question.
if(customControls.InvoHudButton(Rect(10, Screen.height - 100, 93, 95), playerInvo.GetItemCount(InventoryItem.$$anonymous$$ANAPOTION), manapotionImage, "Click To Use a $$anonymous$$ana Potion"))
use print to debug what is happening, print("is clicked"); in the hudbutton condition to see if it is triggered, print manapotionimage file location, print anything you can to see where all the things are. sorry i dont know GUI yet. the error message is a simple one which sais that unity doesnt recognise the asset you want it to load, because it is not at the asset loaded location you specified. if you call an object that isnt there it will give you null reference, which means your referenced item isnt referenced properly as a variable.