How to only pickup item when pressing a key?

I have a pickup item script but how do i make it so it only picks up when i press “E”?
heres the script:

using UnityEngine;

public class Pickup : MonoBehaviour
{
    private Inventory inventory; //The inventory
    public GameObject itemButton; //The ui icon for the item

    public GameObject icon; //The icon in game
    public GameObject itemInGame; //The actual item
    public Transform itemPosition; //The position where the item should go
    public Transform itemHolder; //The item holder
   
    // Start is called before the first frame update
    void Start()
    {
        inventory = GameObject.FindGameObjectWithTag("Inventory").GetComponent<Inventory>();
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.CompareTag("Player")) //If the player collides with the icon hitbox
        {
            for (int i = 0; i < inventory.slots.Length; i++) //Run a for loop for every inventory slot
            {
                if(inventory.isFull[i] == false) //If the slot isn't full
                {
                    //Item can be picked up and added to inventory.
                    inventory.isFull[i] = true; //Set the slot to be full
                    Instantiate(itemButton, inventory.slots[i].transform, false); //Instantiate the ui icon

                    icon.SetActive(false); //Turn off the icon
                    itemInGame.SetActive(true); //And replace it with the actual gun.
                    transform.position = itemPosition.position; //Set the item position to the correct position on the player
                    transform.SetParent(itemHolder); //Make it a child of the player
                    transform.localRotation = Quaternion.identity; //Make the rotation 0, 0, 0
                    break; //Stop running the for loop.
                }
            }
        }
    }
}

i think i might’ve added too many comments

also how do i drop item when pressing q, heres the script

using UnityEngine;

public class Slot : MonoBehaviour
{
    private Inventory inventory;
    public int i;

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

    private void Update()
    {
        if(transform.childCount <= 0)
        {
            inventory.isFull[i] = false;
        }
    }

    public void DropItem()
    {
        foreach(Transform child in transform)
        {
            GameObject.Destroy(child.gameObject);
        }
    }
}

you will be changing OnTriggerEnter2D to OnTriggerStay2D with if(Input) be the first and all the same code go inside

No such thing as too many comments if it helps you.

You need to check for input in Update. So with OnTriggerEnter2D you simply store whatever item you come within range. Then when they hit a key, in Update you can trigger the code to pick it up. If they Exit the trigger without picking it up, you can unregister the stored “pickup target”.

Again, in Update, check for input to drop an item also.

should the if(input) be the last if statmeent because the for loop might not finish running if i only press for a split second

how would i call ontriggerenter/stay in update?

if statement will finish when every code inside are done or you can force if statement to finish by using return;

you cant, but this is how you do it

using UnityEngine;
public class Pickup : MonoBehaviour
{
    private Inventory inventory; //The inventory
    public GameObject itemButton; //The ui icon for the item
    public GameObject icon; //The icon in game
    public GameObject itemInGame; //The actual item
    public Transform itemPosition; //The position where the item should go
    public Transform itemHolder; //The item holder

    private bool isReadyTopickUp = false;
 
    // Start is called before the first frame update
    void Start()
    {
        inventory = GameObject.FindGameObjectWithTag("Inventory").GetComponent<Inventory>();
    }

private void Update()
{
   if(Input.GetKeyDown(KayCode.E) && isReadyTopickUp)
   {
       ProcessPickUp();
       isReadyTopickUp = false;
    }
}

private void OnTriggerEnter2D(Collider2D collision)
{
   if(collision.CompareTag("Player"))
   {
      isReadyTopickUp = true;
   }
}

private void OnTriggerExit2D(Collider2D collision)
{
   if(collision.CompareTag("Player"))
   {
      isReadyTopickUp = false;
   }
}
    private void ProcessPickUp()
    {
        if(collision.CompareTag("Player")) //If the player collides with the icon hitbox
        {
            for (int i = 0; i < inventory.slots.Length; i++) //Run a for loop for every inventory slot
            {
                if(inventory.isFull[i] == false) //If the slot isn't full
                {
                    //Item can be picked up and added to inventory.
                    inventory.isFull[i] = true; //Set the slot to be full
                    Instantiate(itemButton, inventory.slots[i].transform, false); //Instantiate the ui icon
                    icon.SetActive(false); //Turn off the icon
                    itemInGame.SetActive(true); //And replace it with the actual gun.
                    transform.position = itemPosition.position; //Set the item position to the correct position on the player
                    transform.SetParent(itemHolder); //Make it a child of the player
                    transform.localRotation = Quaternion.identity; //Make the rotation 0, 0, 0
                    break; //Stop running the for loop.
                }
            }
        }
    }
}

Edit Error:

 private void ProcessPickUp()
    {
            for (int i = 0; i < inventory.slots.Length; i++) //Run a for loop for every inventory slot
            {
                if(inventory.isFull[i] == false) //If the slot isn't full
                {
                    //Item can be picked up and added to inventory.
                    inventory.isFull[i] = true; //Set the slot to be full
                    Instantiate(itemButton, inventory.slots[i].transform, false); //Instantiate the ui icon
                    icon.SetActive(false); //Turn off the icon
                    itemInGame.SetActive(true); //And replace it with the actual gun.
                    transform.position = itemPosition.position; //Set the item position to the correct position on the player
                    transform.SetParent(itemHolder); //Make it a child of the player
                    transform.localRotation = Quaternion.identity; //Make the rotation 0, 0, 0
                    break; //Stop running the for loop.
                }
            }
    }

use Input.GetKeyDown or Up

i added it, it says that the i++ on line 51 is unreachable, help!

using UnityEngine;

public class Pickup : MonoBehaviour
{
    private Inventory inventory; //The inventory
    public GameObject itemButton; //The ui icon for the item

    public GameObject icon; //The icon in game
    public GameObject itemInGame; //The actual item
    public Transform itemPosition; //The position where the item should go
    public Transform itemHolder; //The item holder

    private bool isReadyToPickUp = false;
   
    // Start is called before the first frame update
    void Start()
    {
        inventory = GameObject.FindGameObjectWithTag("Inventory").GetComponent<Inventory>();
    }

    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.E) && isReadyToPickUp)
        {
            ProcessPickUp();
            isReadyToPickUp = false;

        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Player"))
        {
            isReadyToPickUp = true;
            Debug.Log("Ready to pick up.");
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.CompareTag("Player"))
        {
            isReadyToPickUp = false;
            Debug.Log("Exited hitbox");
        }
    }

    private void ProcessPickUp()
    {
        for (int i = 0; i < inventory.slots.Length; i++) //Run a for loop for every inventory slot
        {
            //Item can be picked up and added to inventory.
            inventory.isFull[i] = true; //Set the slot to be full
            Instantiate(itemButton, inventory.slots[i].transform, false); //Instantiate the ui icon

            icon.SetActive(false); //Turn off the icon
            itemInGame.SetActive(true); //And replace it with the actual gun.
            transform.position = itemPosition.position; //Set the item position to the correct position on the player
            transform.SetParent(itemHolder); //Make it a child of the player
            transform.localRotation = Quaternion.identity; //Make the rotation 0, 0, 0
            break; //Stop running the for loop.
        }
    }
}

help

You have a break in your for loop with no conditions, so it can’t ever loop. It runs once and ends. Also, make sure you’re reviewing your code to see for these logic errors.

1 Like

i havent use break; inside a loop before, i still think about it if break messing the loop up. have you try without the break; will your pickup code still working?

great, it works but now it fills up EVERY SINGLE INVENTORY SLOT AND THEN I CAN STACK ITEMS WTF

i just got rid of the whole entire thing

replace break; with return;

still unreachable

ok, now only way to stop loop is to set “i” to inventory.slots.Length;

i = inventory.slots.Length;
no more break or return.

OMG I FIGURED IT OUT.
i was missing the if(inventory.isFull == false) statement pog
@Putcho thanks for helping me out!

1 Like

i was about to rewrite the whole pick up loop for you, good job you found the way:)

lol well good thing i figured it out! wouldve hated to make u write a whole entire script for me :stuck_out_tongue: :smile:

1 Like