Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

2D Top Down game Inventory Help Wanted

Discussion in '2D' started by RedXstyle, Mar 23, 2021.

  1. RedXstyle

    RedXstyle

    Joined:
    Dec 11, 2020
    Posts:
    1
    I've followed a tutorial on an inventory script and item script that works just fine , but when it comes to the item getting to inventory it won't put it there, instead it is blocked in place . These are my scripts so ;
    This is the Inventory script:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Inventory : MonoBehaviour
    {
    private bool inventoryEnabled;
    public GameObject inventory;

    private int allSlots;
    private int enabledSlots;
    private GameObject[] slot;

    public GameObject slotHolder;

    void Start ()
    {
    allSlots = 24;
    slot = new GameObject[allSlots];
    for(int i = 0; i < allSlots; i++)
    {
    slot = slotHolder.transform.GetChild(i).gameObject;

    if(slot.GetComponent<Slot>().item == null)
    slot.GetComponent<Slot>().empty = true;
    }
    }

    void Update()
    {
    if(Input.GetKeyDown(KeyCode.B))
    inventoryEnabled = !inventoryEnabled;

    if(inventoryEnabled == true)
    {
    inventory.SetActive(true);
    }
    else
    {
    inventory.SetActive(false);
    }
    }

    private void OnTriggerEnter(Collider other)
    {
    if(other.tag == "Item")
    {
    GameObject itemPickedUp = other.gameObject;
    Item item = itemPickedUp.GetComponent<Item>();

    AddItem(itemPickedUp, item.ID, item.type, item.description, item.icon);
    }
    }

    void AddItem(GameObject itemObject, int itemID, string itemtype, string itemDescription, Sprite itemIcon)
    {
    for (int i = 0; i < allSlots; i++)
    {
    if(slot.GetComponent<Slot>().empty)
    {
    itemObject.GetComponent<Item>().pickedUp = true;

    slot.GetComponent<Slot>().item = itemObject;
    slot.GetComponent<Slot>().icon = itemIcon;
    slot.GetComponent<Slot>().type = itemtype;
    slot.GetComponent<Slot>().ID = itemID;
    slot.GetComponent<Slot>().description = itemDescription;

    itemObject.transform.parent = slot.transform;
    itemObject.SetActive(false);

    slot.GetComponent<Slot>().UpdateSlot();
    slot.GetComponent<Slot>().empty = false;
    }
    }
    }
    }


    This is the item script:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Item : MonoBehaviour
    {
    public int ID;
    public string type;
    public string description;
    public Sprite icon;
    public bool pickedUp;
    public bool equipped;
    }

    and this is the slot manager in the inventory script :
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;


    public class Slot : MonoBehaviour
    {
    public GameObject item;
    public bool empty;
    public Sprite icon;
    public int ID;
    public string type;
    public string description;
    public Transform slotIconGO;


    public void Start()
    {
    slotIconGO = transform.GetChild(0);
    }


    public void UpdateSlot()
    {
    slotIconGO.GetComponent<Image>().sprite = icon;
    }
    }