0
\$\begingroup\$

Hello my inventory system picks up one item but does not pickup any more to fill the other two slots. It is saying the whole inventory is full when it should be one slot is full. Here is my code for the inventory and this is attached to the player.

public class MedInventory : MonoBehaviour
{
    public bool[] isFull;
    public GameObject[] MedkitSlot;
    public GameObject[] BandageSlot;
    public GameObject[] CoffeeSlot;
}

This next script is attached to the items you pickup.

    public class MedPickup : MonoBehaviour
{
    private MedInventory medInv;
    public GameObject itemButton;
    private void Start()
    {
        medInv = GameObject.FindGameObjectWithTag("Player").GetComponent<MedInventory>();
        itemButton.SetActive(false);
    }

    void OnTriggerStay2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            GameObject[] targetSlot = new GameObject[0];
            bool validItem = true;

            switch (gameObject.tag)
            {
                case "medkit": targetSlot = medInv.MedkitSlot; break;
                case "bandage": targetSlot = medInv.BandageSlot; break;
                case "coffee": targetSlot = medInv.CoffeeSlot; break;
                default: validItem = false; break;
            }

            if (validItem)
            {
                for (int i = 0; i < targetSlot.Length; ++i)
                {
                    if (!medInv.isFull[i])
                    {
                        medInv.isFull[i] = true;
                        itemButton.SetActive(true);
                        Instantiate(itemButton, targetSlot[i].transform, false);
                        Destroy(gameObject);
                        break;
                    }
                }
            }
        }
    }
}
\$\endgroup\$
8
  • 1
    \$\begingroup\$ Well, you are only checking if your medInv is full but you should check if the target slot is full. Your isFull code is missing as well. \$\endgroup\$
    – Zibelas
    Commented Oct 31, 2020 at 13:01
  • \$\begingroup\$ How would you do that? \$\endgroup\$
    – user144438
    Commented Oct 31, 2020 at 13:26
  • \$\begingroup\$ There is so much wrong with the code on many places, you should first read up a bit about arrays and what they are or for what they are used. Second - how many items can you stack into each slot? Are you setting it in the inspector or can you hold only one item of each? (medkit, bandage, coffee) \$\endgroup\$
    – Zibelas
    Commented Oct 31, 2020 at 15:44
  • \$\begingroup\$ Why is this code on your pickup object at all? When you pick up a coffee cup, is it because you decided you wanted some coffee? Or did the coffee cup decide that for you and insert itself into your hand? \$\endgroup\$
    – DMGregory
    Commented Oct 31, 2020 at 17:50
  • \$\begingroup\$ Thanks sorry if its messy and wrong im just a begginer \$\endgroup\$
    – user144438
    Commented Oct 31, 2020 at 22:53

1 Answer 1

0
\$\begingroup\$

First, let's simplify your pickup. We'll use an enumeration to denote our various inventory item types, so we're not constrained by the tag system and don't need to do messy string marshalling and comparisons.

public enum InventoryItemType : int {
    Medkit,
    Bandage,
    Coffee
}

Now your pickup class can be a "Plain Old Data Structure" - a coffee cup takes no actions on its own, so it needs no methods.

public class Pickup : MonoBehaviour {
    public InventoryItemType type;
    public RectTransform inventoryButtonPrefab;
}

Notice how now, we don't need every pickup in the game to hold a reference to the inventory UI - that's not the coffee cup's job!

The inventory can be managed in one place, by a script on the player character. (Or on each player character, allowing you to support multiplayer. Or you could allow NPCs to pick up items, etc. - much more flexibility this way)

public class PlayerInventory : MonoBehaviour {
    // For convenience, let's make a type that bundles together
    // the UI slot for our inventory item, and the item that's in it.
    [System.Serializable]
    public struct InventorySlot {
        public RectTransform container;
        public RectTransform contents;
    }

    public InventorySlot[] MedkitSlots;
    public InventorySlot[] BandageSlots;
    public InventorySlot[] CoffeeSlots;

    void OnTriggerEnter2D(Collider2D other) {
        // We test whether this object has the component we care about,
        // so it's free to use tags for other purposes.
        if(!other.TryGetComponent(out Pickup pickup))
            return;

        // No need to initialize this with a value we're about to overwrite anyway.
        InventorySlot[] inventory;
        switch(pickup.type) {
            case InventoryItemType.Medkit:  inventory = MedkitSlots;  break;
            case InventoryItemType.Bandage: inventory = BandageSlots; break;
            case InventoryItemType.Coffee:  inventory = CoffeeSlots;  break;
            default: 
                Debug.LogErrorFormat("Unknown pickup type {0}", pickup.type);
                return;
        }

        for(int i = 0; i < inventory.Length; i++) {
            var slot = inventory[i];
            if(slot.contents != null)
                continue;

            slot.contents = Instantiate(pickup.inventoryButtonPrefab, slot.container, false);
            inventory[i] = slot; // Save our modifications to this slot.
            Destroy(other.gameObject);
            return;
        }

        Debug.LogFormat("Can't pick up {0}, inventory slots all full.", pickup.name);
    }
}
\$\endgroup\$
5
  • \$\begingroup\$ Okay so where should the Button prefabs go? In the center of the UI? at 0,0? These buttons look like the items you pick up. \$\endgroup\$
    – user144438
    Commented Nov 1, 2020 at 12:21
  • \$\begingroup\$ Also only the medkit is working when i put it at 0,0 in the UI or the middle of the screen it goes to the slot. But the bandage and coffee just go to a random place on the screen. Also when I click play the when i put the button prefabs in the middle of the UI they appear in the game too \$\endgroup\$
    – user144438
    Commented Nov 1, 2020 at 12:37
  • \$\begingroup\$ The button prefabs are not part of your scene. They're assets in your project folder, as prefabs normally are. Note that this isn't anything new in this version, I just took the itemButton you were already using and renamed it. \$\endgroup\$
    – DMGregory
    Commented Nov 1, 2020 at 12:48
  • \$\begingroup\$ Okay so now all the items go to there slots except the bandage. Do you know why? \$\endgroup\$
    – user144438
    Commented Nov 1, 2020 at 12:54
  • \$\begingroup\$ Okay nevermind it works now the bandage prefab was not at 0,0 \$\endgroup\$
    – user144438
    Commented Nov 1, 2020 at 12:55

You must log in to answer this question.