- Home /
 
Need help with my 2D inventory slot system
Hi I'm doing a game where you have to choose and put the objects on your wagon and make it to the end. I'm doing an inventory system for the objects. Here I have a video of the game right now: https://youtu.be/exkOyCMIbuI I have little experience with coding in Unity, but I've come up with this code:
public class SlotManager : MonoBehaviour { public int Quantity; public GameObject Item; public TextMeshProUGUI Text; public Vector3 Coordinates; public char ItemType;
 private string showScore;
 private bool spawn;
 void Start ()
 {
     Instantiate(Item, Coordinates, transform.rotation);
 }
 
 void Update ()
 {
     Text.SetText(Quantity.ToString());
 }
 void OnMouseOver()
 {
     spawn = true;
 }
 void OnMouseExit()
 {
     spawn = false;
 }
 void OnMouseDown()
 {
     if (spawn == true && Quantity == 1)
     {
         Quantity -= 1;
     } else if (spawn == true && Quantity > 0)
     {
         Instantiate(Item, Coordinates, transform.rotation);
         Quantity -= 1;
     }
     spawn = false;
 }
 
               }
Each inventory slot is a prefab. The code spawns another prefab every time I click onto the slot and decreases the quantity, making this nice effect. The problem is that I don't know how to make items get back onto the inventory slot. The items falling from the slots is an easy fix btw, but I didn't wanted to make anything untill I had this covered.
I know It's easy stuff but I'm such a noob help is SOOOO appreciated thanks. If the solution implies changing all the inventory system then that's what I'll do.
Answer by N-8-D-e-v · Jun 18, 2020 at 02:32 PM
Try using lists, the Unity API doesn't have a page for these, but they are like arrays, but are used when adding and removing things https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=netframework-4.6
Your answer