Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Mar 28, 2016 at 10:05 AM by gamingwithxd for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by gamingwithxd · Mar 28, 2016 at 12:18 AM · c#instantiatedestroyinventory system

[Solved]Instantiated item won't get destroyed until "picked up" again.

So I been working on an inventory system on my own (first one I ever done). I have it working where the item gets picked up and placed in the right slot of the list. I also gave the ability to drop the item by pressing 'Q' which also works. My problem is that if I have two items and I drop both, I am able to pick up the first item like normal but the second item I am able to pick up the item but the object doesn't get destroyed until I pick it up again which results in having two of that item instead of one.

This is the code that places item in my inventory:

 if (Physics.Raycast(InteractRay, out hit, InteractDistance))
         {
             //If it hits a fruit collect if there is enough space
             if (hit.collider.tag == "Pickup" && Input.GetKeyDown(KeyCode.E))
             {
                 //if item is in inventory already
                 if (gameObject.GetComponent<Inventory_Script>().HotBar.Contains(hit.transform.GetComponent<Interaction_Item>().Collect()))
                 {
                     //if below inventory max count then collect
                     if (gameObject.GetComponent<Inventory_Script>().HotBarNumberOfItems[gameObject.GetComponent<Inventory_Script>().HotBar.IndexOf(hit.transform.GetComponent<Interaction_Item>().Collect())] < 10)
                     {
                         gameObject.GetComponent<Inventory_Script>().HotBarNumberOfItems[gameObject.GetComponent<Inventory_Script>().HotBar.IndexOf(hit.transform.GetComponent<Interaction_Item>().Collect())] += 1;
                         Destroy(hit.collider.gameObject);
                     }
                 }
                 //if There is space in inventory and item is not already collected, add to inventory
                 else if (!gameObject.GetComponent<Inventory_Script>().HotBar.Contains(hit.transform.GetComponent<Interaction_Item>().Collect()) && gameObject.GetComponent<Inventory_Script>().HotBar.Contains(null))
                 {
                     foreach (GameObject empty in gameObject.GetComponent<Inventory_Script>().HotBar)
                     {
                         if (empty == null)
                         {
                             gameObject.GetComponent<Inventory_Script>().HotBar[gameObject.GetComponent<Inventory_Script>().HotBar.IndexOf(empty)] = hit.transform.gameObject.GetComponent<Interaction_Item>().Collect() as GameObject;
                             gameObject.GetComponent<Inventory_Script>().HotBarNumberOfItems[gameObject.GetComponent<Inventory_Script>().HotBar.IndexOf(empty)-1] += 1;
                             gameObject.GetComponent<Inventory_Script>().showItem();
                             Destroy(hit.collider.gameObject);
                             break;
                         }
                     }
                 }
 
                 if (!gameObject.GetComponent<Inventory_Script>().HotBar.Contains(null))
                 {
                     //if item is in inventory already
                     if (gameObject.GetComponent<Inventory_Script>().Inventory.Contains(hit.transform.GetComponent<Interaction_Item>().Collect()))
                     {
 
                         //if below inventory max count then collect
                         if (gameObject.GetComponent<Inventory_Script>().InventoryNumberOfItems[gameObject.GetComponent<Inventory_Script>().Inventory.IndexOf(hit.transform.GetComponent<Interaction_Item>().Collect())] < 10)
                         {
                             gameObject.GetComponent<Inventory_Script>().InventoryNumberOfItems[gameObject.GetComponent<Inventory_Script>().Inventory.IndexOf(hit.transform.GetComponent<Interaction_Item>().Collect())] += 1;
                             Destroy(hit.transform.gameObject);
                         }
                     }
                     //if There is space in inventory and item is not already collected, add to inventory
                     else if (!gameObject.GetComponent<Inventory_Script>().Inventory.Contains(hit.transform.GetComponent<Interaction_Item>().Collect()) && gameObject.GetComponent<Inventory_Script>().Inventory.Contains(null))
                     {
                         foreach (GameObject empty in gameObject.GetComponent<Inventory_Script>().Inventory)
                         {
                             if (empty == null)
                             {
                                 gameObject.GetComponent<Inventory_Script>().Inventory[gameObject.GetComponent<Inventory_Script>().Inventory.IndexOf(empty)] = hit.transform.gameObject.GetComponent<Interaction_Item>().Collect() as GameObject;
                                 gameObject.GetComponent<Inventory_Script>().InventoryNumberOfItems[gameObject.GetComponent<Inventory_Script>().Inventory.IndexOf(empty)] += 1;
                                 gameObject.GetComponent<Inventory_Script>().showItem();
                                 Destroy(hit.transform.gameObject);
                                 break;
                             }
                         }
                     }
                 }
             }
         }

This is my dropping code:

  //Drop item from inventory
         if (Input.GetKeyDown(KeyCode.Q))
         {
             if (gameObject.GetComponent<Inventory_Script>().HotBarNumberOfItems[InventorySelection] > 0)
             {
                 GameObject Item;
                 Item = Instantiate(gameObject.GetComponent<Inventory_Script>().HotBar[InventorySelection], transform.position, transform.rotation) as GameObject;
                 Item.GetComponent<Rigidbody>().velocity = transform.TransformDirection(Vector3.forward * 3);
                 gameObject.GetComponent<Inventory_Script>().HotBarNumberOfItems[InventorySelection] -= 1;
 
                 if (gameObject.GetComponent<Inventory_Script>().HotBarNumberOfItems[InventorySelection] == 0)
                 {
                     gameObject.GetComponent<Inventory_Script>().HotBar[InventorySelection] = null;
                     gameObject.GetComponent<Inventory_Script>().showItem();
                 }
             }
         }

This is the function Collect() that you see in my inventory code:

 public GameObject Collect()
     {
         return Resources.Load(prefab) as GameObject;
     }

This is the the script Inventory_Script where I create the lists:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Inventory_Script : MonoBehaviour {
 
     public GameObject Item;
     public List<GameObject> Inventory = new List<GameObject>();
     public List<GameObject> HotBar = new List<GameObject>();
     public List<int> InventoryNumberOfItems = new List<int>();
     public List<int> HotBarNumberOfItems = new List<int>();
 
     // Use this for initialization
     void Start () {
 
         for (int i = 0; i < 10; i++)
         {
             Inventory.Add(null);
             InventoryNumberOfItems.Add(0);
         }
 
         for (int i = 0; i < 5; i++)
         {
             HotBar.Add(null);
             HotBarNumberOfItems.Add(0);
         }
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     public void showItem()
     {
         if (gameObject.GetComponent<Inventory_Script>().HotBar[gameObject.GetComponent<Player_Interaction>().InventorySelection] == null)
         {
             Item.GetComponent<MeshFilter>().mesh = null;
         }
         Item.GetComponent<MeshFilter>().mesh = gameObject.GetComponent<Inventory_Script>().HotBar[gameObject.GetComponent<Player_Interaction>().InventorySelection].gameObject.GetComponent<MeshFilter>().sharedMesh;
         Item.GetComponent<MeshRenderer>().material = gameObject.GetComponent<Inventory_Script>().HotBar[gameObject.GetComponent<Player_Interaction>().InventorySelection].gameObject.GetComponent<MeshRenderer>().sharedMaterial;
         Item.transform.localScale = gameObject.GetComponent<Inventory_Script>().HotBar[gameObject.GetComponent<Player_Interaction>().InventorySelection].transform.localScale;
     }
 }


Like I said if I drop both items and pick up the second one, it doesn't get destroyed until I pick it up again giving two instead of one. This happens with any item regardless of type (ex. Apples or Oranges). This can be reproduced easily. Thanks for the help.

P.S Inventory code and dropping code are in update()

Comment
Add comment · Show 3
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 Fredex8 · Mar 28, 2016 at 12:26 AM 0
Share

You really need to look at caching references. That code is a little bit too dense to read easily...

 private Inventory_Script inventoryScript;
 
 void Start()
 {
      inventoryScript = gameObject.GetComponent<Inventory_Script>();
 }

Cache the reference on start like that then replace every instance of gameObject.GetComponent<Inventory_Script>() just with inventoryScript. It's going to make it far easier for you to write, read and debug.

avatar image gamingwithxd Fredex8 · Mar 28, 2016 at 08:28 AM 0
Share

Well I did that and I seem to have found out the problem (not in the code itself but through testing it in game. When I pick up the instantiated object and I have no items, if I don't have my InventorySelection variable where the item is gonna appear in the inventory then it will not get destroyed but if for example I have no items and InventorySelection is on 0 and I pick up thhe object then it works.

avatar image gamingwithxd Fredex8 · Mar 28, 2016 at 10:04 AM 0
Share

Fixed it, it was my showItem() function running before destroying the object. Something so simple causing a huge headache.

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by gamingwithxd · Mar 28, 2016 at 02:59 PM

I Solved the problem. after writing tons of while loops and for loops it was actually the fact that I ran the showItem() function before destroying the object. Simple mistake.

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why could I delete my clones onlY in the same order of instantiation? 2 Answers

What's a better way to code in chunks 1 Answer

How to destroy an instantiated prefab object and keep instantiating it 1 Answer

C# override a destroyed GameObject in a List 1 Answer

How to determine and delete the oldest instantiated object? 2 Answers


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