Need help with two codes being called at once

I am working on a simple weapon pickup script and am having some trouble.

When i Press my E button to call for my Pickup() method, it also calls for my Drop() method.

I would like to use the same button to pickup and drop my weapons but I’m not sure how to fix this. I’ve tried using an IEnumerator to wait half a second then to make the isheld true but that didnt seem to work.

Please if anyone has any advice or suggestions that’ll be very helpful!!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ItemPickup : MonoBehaviour
{
    public bool PickupAllowed = false;
    public float Pickupradius;
    public Transform PickupSpot;
    public LayerMask PlayerLayer;
    public Transform NewPosition;
    private bool IsHeld = false;
    //private bool iscurrentlyholding = false;




    public GameObject PlayerParent;

   


    void Update()
    {
        //check if the items in range
        PickupCircle();

        //pickup the item
        if (PickupAllowed == true)
        {
            if (Input.GetKeyDown(KeyCode.E) && IsHeld == false)
            {
                Pickup();

                Debug.Log("Pickup is called");
            }


            if (Input.GetKeyUp(KeyCode.E))
            {
                IsHeld = true;
            }
        }


        //drop the item
        if (IsHeld == true)
        {
            if(Input.GetKeyDown(KeyCode.E))
            {
                Drop();

                Debug.Log("Drop is called");
            }


            if (Input.GetKeyUp(KeyCode.E))
            {
                IsHeld = false;
                Debug.Log(IsHeld);
            }
        }
    }


    private void PickupCircle()
    {
        if(Physics2D.OverlapCircle(PickupSpot.position, Pickupradius, PlayerLayer))
        {
            PickupAllowed = true;
        }
    }

    private void OnDrawGizmos()
    {
        Gizmos.DrawWireSphere(PickupSpot.position, Pickupradius);
    }



    private void Pickup()
    {
        gameObject.transform.position = new Vector2(NewPosition.position.x, NewPosition.position.y);

        gameObject.transform.parent = PlayerParent.transform;

    }


   
    public void Drop()
    {
        gameObject.transform.parent = null;

        gameObject.transform.position = new Vector2(transform.position.x, transform.position.y);
    }
}

You need to look at if else statements to control what part runs.
Also just a friendly note, general design is variables start with a lower case and Methods start with an uppercase to define the difference between the two easier. Not that what you have breaks anything.

if(PickupAllowed)
{
  //code
}
else if(IsHeld)
{
   //Other code
}

The issue as you have it is you pickup the item and then drop it right after because you’re setting isHeld = true. So it just runs the next if right after. Also to note, I don’t see where you set PickupAllowed to false, so if you don’t do that, your isHeld part of the code isn’t going to run as it will always hit the first if.

Awesome thank you! yeah sounds like a simple rookie mistake. I believe i made the PickupAllowed false by default and it becomes true whenever you enter the circle radius of the item

Also I just ran into a new problem. the if /else if worked and i gave the object a rigid body so i could have it fall to the floor when its dropped. But the problem im running into now is when i move with the object it doesnt follow and stay on my characters body under dynamic on the rigidbody2D and when i use kinematic it doesnt fall when dropping it.

When you drop your object, change it’s mode?

Here’s a simple example:

Rigidbody rbody = this.gameObject.GetComponent<Rigidbody>();
rbody.isKinematic = false;