Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Bincredible · Sep 24, 2014 at 11:53 PM · instantiateprefabarraydestroyinventory

Destroying Instantiated Prefab

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[i].icon,"Name : " + mainInventory[i].name + "\n" + "Description : " + 
                 mainInventory[i].desc + "\n" + "Rarity : " + mainInventory[i].rarity.ToString()))){
 /*************Check item type*************/
                         if(mainInventory[i].itemType == mainInventory[i].itemType.handItem){
                             currentlyEquiped[0] = mainInventory[i];
                             handItem = Instantiate(currentlyEquiped[0].physicalItem, handPos.position, handPos.rotation);
                             handItem.parent = handPos;
                             //Add rotation    
                         }
                     
                     mainInventory.RemoveAt(i);
                 }    
             }else{
                 GUI.Box(Rect(23+(52*x),Screen.height-340+(52*y),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]);
     }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image 767_2 · Sep 25, 2014 at 07:30 AM 0
Share

you mean you checked and it wasn't null

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by fotaras · Sep 25, 2014 at 02:51 AM

try Destroy(handItem); And see if it works

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bincredible · Sep 25, 2014 at 07:21 AM 0
Share

I just get an error saying "NullReferenceException: Object reference not set to an instance of an object" and the object isn't destroyed.

avatar image fotaras · Sep 25, 2014 at 07:39 AM 0
Share

the handItem Variable is not declared in the top of tha class? if not the handItem you use int the instanciate is diferent to the one you use in destroy

avatar image fotaras · Sep 25, 2014 at 07:41 AM 0
Share

@Bincredible Declare it in the top and try again

avatar image Bincredible · Sep 25, 2014 at 03:52 PM 0
Share

Yeah, declaring it at the top works. I added var handItem : Transform; at the top and got rid of the var in front of the Instantiate();

avatar image fotaras · Sep 27, 2014 at 11:48 AM 0
Share

@Bincredible After you have solved your problem mark your questions as answered

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Prefabs into array than delete them when user interacts 1 Answer

Parenting an instantiated prefab. 1 Answer

how to destroy camera instatiated from prefab? 0 Answers

Which is faster for a pool of objects, instantiating and adding to an array or loading pre-made objects to an array? 1 Answer

Instantiating prefabs: "The object of type GameObject has been destroyed". 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges