0
\$\begingroup\$

Ok so i made this inventory script but the items are not picking up and not appearing in the inventory. Im new to c# so that is why im asking this question also ive never done an inventory before. I want the medkit to go in the medkit slot. And bandage in the bandage slot and so on. Here are my scipts.

Here is my inventory script attached to the player.

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

Here is the medical pickup script attached to the item pickup.

public class MedPickup : MonoBehaviour
{
    private MedInventory medInv;
    public GameObject itemButton;

    private void Start()
    {
        medInv = GameObject.FindGameObjectWithTag("Player").GetComponent<MedInventory>();
    }

    void OnTriggerStay2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            if (other.CompareTag("medkit"))
            {
                for (int i = 0; i < medInv.MedkitSlot.Length; i++)
                {
                    if (medInv.isFull[i] == false)
                    {
                        medInv.isFull[i] = true;
                        Instantiate(itemButton, medInv.MedkitSlot[i].transform, false);
                        Destroy(gameObject);
                        break;
                    }
                }
            }

            if (other.CompareTag("bandage"))
            {
                for (int i = 0; i < medInv.BandageSlot.Length; i++)
                {
                    if (medInv.isFull[i] == false)
                    {
                        medInv.isFull[i] = true;
                        Instantiate(itemButton, medInv.BandageSlot[i].transform, false);
                        Destroy(gameObject);
                        break;
                    }
                }
            }

            if (other.CompareTag("coffee"))
            {
                for (int i = 0; i < medInv.CoffeeSlot.Length; i++)
                {
                    if (medInv.isFull[i] == false)
                    {
                        medInv.isFull[i] = true;
                        Instantiate(itemButton, medInv.CoffeeSlot[i].transform, false);
                        Destroy(gameObject);
                        break;
                    }
                }
            }

            if (other.CompareTag("syringe"))
            {
                for (int i = 0; i < medInv.SyringeSlot.Length; i++)
                {
                    if (medInv.isFull[i] == false)
                    {
                        medInv.isFull[i] = true;
                        Instantiate(itemButton, medInv.SyringeSlot[i].transform, false);
                        Destroy(gameObject);
                        break;
                    }
                }
            }
        }
    }
}

Please help and thanks!

\$\endgroup\$
  • \$\begingroup\$ 1. Are there any errors? 2. Is the casing on your tags right? I notice some are capitalized and some aren't. 3. You are repeating yourself a lot in this code. See about splitting it into more methods so you can avoid that? 4. Is it actually the player that is supposed to have ALL these tags? \$\endgroup\$ – Weckar E. Oct 29 at 0:50
  • \$\begingroup\$ 1. There are no errors. 2. Yes I just checked again all the casing and spelling is right. 3. I will look at that. 4. The player should only have the Player tag I think. \$\endgroup\$ – user144438 Oct 29 at 1:02
  • \$\begingroup\$ "if (other.CompareTag("medkit"))" I do believe you are checking the player for that tag, are you not? "other" being what you collided with? \$\endgroup\$ – Weckar E. Oct 29 at 1:09
  • \$\begingroup\$ Oh how would i call that tag function for an item pickup? \$\endgroup\$ – user144438 Oct 29 at 1:14
0
\$\begingroup\$

It looks like you are checking the same "other" GameObject in your OnCollisionStay function for two different tags. This should successfully identify your player (assuming they are tagged as "Player") but will skip all code adding items to their inventory (since the player is not tagged "medkit", "bandage", etc.). You should be able to fix this by checking your pickup gameobjects tag.

I apologize if the code sample below is confusing (I figured since you are new to coding cleaning up the script a little might be helpful as an example), the key take away is that instead of calling gameObject.tag to check the current pickup item type instead of other.ComapreTag. You may also want to look into using interfaces/subclasses for your items since they should probably fit and it would let you avoid having a separate array for each item type.

public class MedPickup : MonoBehaviour
{
    private MedInventory medInv;
    public GameObject itemButton;

    private void Start()
    {
        medInv = GameObject.FindGameObjectWithTag("Player").GetComponent<MedInventory>();
    }

    void OnTriggerStay2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            GameObject[] targetSlot = new GameObject[0]; // need to initialize this to something to avoid compiler error.
            bool validItem = true;

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

            if (validItem)
            {
                for (int i = 0; i < targetSlot.Length; ++i)
                {
                    if (!medInv.isFull[i])
                    {
                        medInv.isFull[i] = true;
                        Instantiate(itemButton, targetSlot[i].transform, false);
                        Destroy(gameObject);
                        break;
                    }
                }
            }
        }
    }
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thanks! This works but only the first item you touch is picking up. Also the Item buttons are appearing over the character at all times. I need those buttons to appear only when its in the players inventory. \$\endgroup\$ – user144438 Oct 29 at 12:30
  • \$\begingroup\$ No worries glad it helps a little even if buggy. Since I copied and reorganized your original code a little I don't think I introduced any new issues. Did you have similar problems with your old code or is this definitely something I introduced? As far as I can tell this should simply add a button each time an object is picked up and parent the button to the targetSlot transform. You may need to move the localPosition or localRotation to match your UI. As for this only working once, you may want to look into using break points to see if other pickups are firing the script when collided. \$\endgroup\$ – Benjamin Danger Johnson Nov 6 at 3:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy