Hello, I am working on an inventory system but I have got a bit of an issue with destroying an instantiated prefab from the scene by clicking a GUI.Button. In my code I have made the item spawn and become a child of the player’s hand so it follows the position. But when I click a different button which is supposed to remove it from the scene I am having issues with getting this to work. I’ve tried Destroy(handItem.gamObject); but I get an error message saying “NullReferenceException: Object reference not set to an instance of an object” I don’t know how to do this and any help would be great! Here’s my code for the inventory, line 92 is where the button is which I want to press to delete the instance of the prefab. And line 61 is where the prefab is being instantiated.
import System.Collections.Generic;
var items : Item[];
var currentlyEquiped : Item[];
//[0 = handItem, 1 = headItem, 2 = shoulder/neckItem, 3 = torsoItem]
var mainInventory : List.<Item> = new List.<Item>();
private var inventoryColumns : int = 3;
private var inventoryRows : int = 5;
var inventoryOpen : boolean = false;
var currentState : String = "Open Inventory";
var equipMenuOpen : boolean = false;
var currentEquipText : String = "Open Equip Menu";
var buttonWidth : int = 50;
var buttonHeight : int = 50;
var handPos : Transform;
function Update(){
if(Input.GetKeyDown(KeyCode.I)){
inventoryOpen = !inventoryOpen;
}
if(inventoryOpen){
currentState = "Close Inventory";
}
if(!inventoryOpen){
currentState = "Open Inventory";
}
if(equipMenuOpen){
currentEquipText = "Close Equip Menu";
}
if(!equipMenuOpen){
currentEquipText = "Open Equip Menu";
}
}
function OnGUI(){
if(GUI.Button(Rect(10,Screen.height-40,120,25),currentState)){
inventoryOpen = !inventoryOpen;
}
if(inventoryOpen){
GUI.Box(Rect(10,Screen.height-375,350,325),"Inventory");
var i : int = 0;
for(var y = 0; y < inventoryRows; y ++){
for(var x = 0; x < inventoryColumns; x ++){
if(i < mainInventory.Count){
if(GUI.Button(Rect(23+(52*x),Screen.height-340+(52*y),50,50),
GUIContent(mainInventory_.icon,"Name : " + mainInventory*.name + "
" + "Description : " +_
mainInventory_.desc + "
" + "Rarity : " + mainInventory.rarity.ToString()))){
/Check item type/
if(mainInventory.itemType == mainInventory.itemType.handItem){
currentlyEquiped[0] = mainInventory;
handItem = Instantiate(currentlyEquiped[0].physicalItem, handPos.position, handPos.rotation);
handItem.parent = handPos;
//Add rotation
}*_
* mainInventory.RemoveAt(i);*
* } *
* }else{*
_ GUI.Box(Rect(23+(52x),Screen.height-340+(52y),50,50),“”);
* }
i++;
}*_
* }*
* //Display the ToolTip*
GUI.Box(Rect(186,Screen.height-340,160,258),“Item Information”);
GUI.Label(Rect(195,Screen.height-320,145,200), GUI.tooltip);
if(GUI.Button(Rect(340,Screen.height-375,20,20),“X”)){
* inventoryOpen = !inventoryOpen;*
}
if(GUI.Button(Rect(10,Screen.height-375,120,20),currentEquipText)){
* equipMenuOpen = !equipMenuOpen;*
}
if(equipMenuOpen){
* GUI.Box(Rect(361,Screen.height-375,180,180),“Equip Menu”);*
//Equipped Items buttons
if(currentlyEquiped[0] != null){
* if(GUI.Button(Rect(370,Screen.height-340,30,30),currentlyEquiped[0].icon)){*
* mainInventory.Add(currentlyEquiped[0]);*
* currentlyEquiped[0] = null;*
* Destroy(handItem.gameObject);*
* }*
}
}
}
}
function AddItem(Name : String){
* if(Name == “CupOfCoffee”){*
* mainInventory.Add(items[0]);*
* }*
* if(Name == “Popsicle”){*
* mainInventory.Add(items[1]);*
* }*
* if(Name == “BasicSword”){*
* mainInventory.Add(items[2]);*
* }*
}
you mean you checked and it wasn't null
– 767_2