- Home /
How can I delete an item from this inventory system?
So I basically followed some YouTube videos to learn how to make inventory systems and I found one I really liked. Here are the following codes:
Spawn.cs using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Spawn : MonoBehaviour
{
public GameObject item;
private Transform player;
public Button button;
private void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
}
public void SpawnDroppedItem()
{
Vector2 playerPos = new Vector2(player.position.x, player.position.y + 3);
Instantiate(item, playerPos, Quaternion.identity);
GameObject.Destroy(gameObject);
}
private void Update()
{
Button btn = button.GetComponent<Button>();
btn.onClick.AddListener(delegate{SpawnDroppedItem();});
}
}
Inventory.cs using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Inventory : MonoBehaviour
{
public GameObject[] inventory = new GameObject[10];
public Button[] InventoryButtons = new Button[10];
public bool itemAdded = false;
public bool buttonPressed;
public void AddItem(GameObject item)
{
// Find the first open slot in the inventory
for (int i = 0; i < inventory.Length; i++)
{
if (inventory [i] == null)
{
inventory[i] = item;
// Update UI
InventoryButtons[i].image.overrideSprite = item.GetComponent<SpriteRenderer>().sprite;
Debug.Log(item.name + " was added.");
itemAdded = true;
// Do something with the object
item.SendMessage("DoInteraction");
break;
}
}
// Inventory is full
if (!itemAdded)
{
Debug.Log("Inventory is full. Item not added.");
}
}
public void RemoveItem()
{
}
}
InteractionObject.cs using System.Collections; using System.Collections.Generic; using UnityEngine;
public class InteractionObject : MonoBehaviour
{
public bool inventory; // If true this object can be stored in inventory
public void DoInteraction()
{
// Picked up and put in inventory
gameObject.SetActive(false);
}
}
PlayerInteract.cs using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerInteract : MonoBehaviour
{
public GameObject currentIntObj = null;
public InteractionObject currentInterObjScript = null;
public Inventory inventory;
private void Update()
{
if (Input.GetKeyDown(KeyCode.E) && currentIntObj)
{
// Check if this object is an interactable and to be stored in inventory
if (currentInterObjScript.inventory)
{
inventory.AddItem(currentIntObj);
}
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Interactable"))
{
Debug.Log(other.name + " is in range");
currentIntObj = other.gameObject;
currentInterObjScript = currentIntObj.GetComponent<InteractionObject>();
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.CompareTag("Interactable"))
{
if (other.CompareTag("Interactable"))
{
currentIntObj = null;
}
}
}
}
So basically I'm trying to get it where if I clicked on the button/slot, the item is removed from the array of buttons and it placed back into the world. I somewhat got it with the SpawnDroppedItem method and using the update method to check if the button was clicked but I can't figure out how to remove it from the list. If someone can help that would be amazing.
Also, if you can answer this question, where and how would I be best able to learn to create an inventory system? Thank you for your time.
Answer by unity_ek98vnTRplGj8Q · Dec 20, 2019 at 04:38 PM
It looks like the script just checks each inventory slot for a null slot when its trying to add something to the inventory. So to remove, just set the slot to null.
public void RemoveItem(GameObject item)
{
//If you already know what the index of the item is, skip this part
int index = -1;
for(int i=0; i<inventory.Length; i++){
if(inventory[i] == item) {
index = i;
break;
}
}
if(index == -1){
Debug.Log("Item not found in inventory");
return;
}
item = inventory[index]; //If you don't already have the item reference (this example script does) then grab it now
inventory[index] = null; //Remove item from inventory
InventoryButtons[index].image.overrideSprite = null; //Clear the button sprite
//Drop the item by doing something like this
Spawn spawn = GetComponent<Spawn>();
spawn.item = item;
spawn.SpawnDroppedItem();
}
But how would I make it know that the button was pressed to have to null the item index?
Does each inventory slot have a seperate button to remove items? If so, change the script to
public void RemoveItem(int index)
{
item = inventory[index];
if(item == null) {
Debug.Log("Inventory slot is already empty");
return;
}
inventory[index] = null; //Remove item from inventory
InventoryButtons[index].image.overrideSprite = null; //Clear the button sprite
//Drop the item by doing something like this
Spawn spawn = GetComponent<Spawn>();
spawn.item = item;
spawn.SpawnDroppedItem();
}
Then in the inspector you can go to the button, add an OnClick event, drag the gameobject with the inventory script attached to it into the box, select Inventory.RemoveItem and give it the index of the slot you want to remove the item from
Your answer
Follow this Question
Related Questions
unity3d simple inventory 1 Answer
[C#] Grouping variables 1 Answer
adding inventory items is half-working 0 Answers
Inventory AddItem help 1 Answer
Object Reference Not Set To An Instance of An Object Error 1 Answer