- Home /
The question is answered, right answer was accepted
[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()
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.
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.
Fixed it, it was my showItem() function running before destroying the object. Something so simple causing a huge headache.
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.
Follow this Question
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