Hey all, first time posting. I have tried to find an answer to this question and have had no luck. I am creating an inventory system where you can pick up any number of items, some of them stack some don’t. I have a general “Loot” script that is attached to each Loot object, and each is it’s own Prefab. Currently, I have a raycast to the mouse position and when you click the button, the object is added to a List Database and the object is destroyed. Then, in your menu, you click on the item and can click “Drop” and I want it to drop the object at the players current position, similar to Fallout or Elder Scrolls. However, when I try to Instantiate the object at the characters position, I get this error:
“The object of type ‘GameObject’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.”
I am assuming that this is because I destroyed the original item, but I reference the Prefab through the Loot script, so I am just not sure what the best way around this is. I really just want it to create a new gameobject prefab where the player is and for that object to inherit the “Loot” script variables. Any ideas?
Sorry, I meant to attach some of the script for you too.
On the player controller:
if (Input.GetMouseButtonDown (0))
{
inventoryDatabase.GetComponent<Inventory_Database>().AddLoot(rayHit.transform.GetComponent<Loot>());
Destroy(rayHit.transform.gameObject);
string inventoryType = inventoryMenuController.GetComponent<inventoryMenuController>().inventoryType;
inventoryMenuController.GetComponent<inventoryMenuController>().INV_SUB_BTN(inventoryType);
}
On the Inventory System script:
invSelectedLootNumber = inventoryNumber + inventorySlot;
Loot loot = Inventory_Database.sortedLoot [invSelectedLootNumber];
//invSelectedLoot = Inventory_Database.sortedLoot [invSelectedLootNumber].physicalLoot;
invSelectedLoot = loot;
and further down in the same script…
public void DropItem()
{
Instantiate (invSelectedLoot.physicalLoot, player.transform.position, player.transform.rotation);
//GameObject newItem = new GameObject ();
//Instantiate(invSelectedLoot, player.transform.position, player.transform.rotation);
//GameObject droppedLoot = Instantiate (invSelectedLoot) as GameObject;
//droppedLoot.transform.position = player.transform.position;
Inventory_Database.allLoot.RemoveAt (invSelectedLootNumber);
INV_SUB_BTN (inventoryType);
}