Update if-statements activate wrong method.

I have this pick up and hold items script, and whenever I try to pick up an object it activates the drop method and teleports the item to the drop position. Would you guys please be so kind as to help me with this? (The Update() void where the methods get activated is at line 50) PS: The seperate quaternions are because I got my (temporary) models form online and they’re all built in different sizes and angles so every quaternion has to be different.

using UnityEngine;
using System.Collections;

namespace CarryingItems {

    public class PickUp : MonoBehaviour {
           
        private float RayCastLength = 2.15F;
        public LayerMask RayMask;
              
        public float OffsetDropPosition = 2;
      
        private Quaternion CharQua;
        private Quaternion KnifeQua;
        private Quaternion CleaverQua;
        private Quaternion BreadKnifeQua;
      
        private GameObject CarryableObject;
        private GameObject Hand;
        private GameObject Character;
        private GameObject CameraMain;
        private GameObject Handle;
        private GameObject HandleExtra;
        private GameObject BladeHolder;
        private GameObject Blade;
      
          
        private bool ItemPickedUp;
              
        void Awake() {
          
            KnifeQua = new Quaternion(13.80f, -23.77f, -81.62f, 1f);
            CleaverQua = new Quaternion(17.86f, -42.52f, -92.53f, 1f);
            BreadKnifeQua = new Quaternion(-17.16f, 167.89f, 90.24f, 1f);
          
            CarryableObject = this.gameObject;
            Hand = GameObject.Find("ItemHand");
            Character = GameObject.Find("PlayerCharacter");
            CameraMain = GameObject.Find("MainCamera");
            Handle = this.gameObject.transform.GetChild(0).gameObject;
            HandleExtra = this.gameObject.transform.GetChild(1).gameObject;
            BladeHolder = this.gameObject.transform.GetChild(2).gameObject;
            Blade = this.gameObject.transform.GetChild(3).gameObject;
        }      
      
        void Start() {
            ItemPickedUp = false;
        }
      
        void Update() {
            if (Input.GetKeyDown("e")) {  
                if(ItemPickedUp == false) {
                    ItemPickUp();
                }
                if(ItemPickedUp == true){
                    ItemDrop();
                }
            }          
        }
      
        void ItemPickUp() {
            if (Physics.Raycast(CameraMain.transform.position, CameraMain.transform.forward, RayCastLength, RayMask.value)) {
                CarryableObject = this.gameObject;
                CarryableObject.transform.parent = Character.transform;
                CarryableObject.GetComponent<Rigidbody>().useGravity = false;
                CarryableObject.transform.position = Hand.transform.position;
                CarryableObject.transform.rotation = Hand.transform.rotation;
                ItemPickedUp = true;
              
                if(Handle != null){
                    Handle.GetComponent<Collider>().enabled = false;
                }
                if(HandleExtra != null){
                    HandleExtra.GetComponent<Collider>().enabled = false;
                }
                if(BladeHolder != null){
                    BladeHolder.GetComponent<Collider>().enabled = false;
                }
                if(Blade != null){
                    Blade.GetComponent<Collider>().enabled = false;
                }
            }
        }
      
        void ItemDrop() {
            CarryableObject.transform.parent = null;
            CarryableObject.transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + OffsetDropPosition);
            CarryableObject.transform.rotation = Character.transform.rotation;
            CarryableObject.GetComponent<Rigidbody>().useGravity = true;
            ItemPickedUp = false;
          
            if(Handle != null){
                Handle.GetComponent<Collider>().enabled = true;
            }
            if(HandleExtra != null){
                HandleExtra.GetComponent<Collider>().enabled = true;
            }
            if(BladeHolder != null){
                BladeHolder.GetComponent<Collider>().enabled = true;
            }
            if(Blade != null){
                Blade.GetComponent<Collider>().enabled = true;
            }
        }  
    }
}

Here you’re picking the item up if it’s not picked ut, then immediately dropping it if it’s picked up.

You’ll need to else-if the second line there:

void Update() {
    if (Input.GetKeyDown("e")) {
        if(ItemPickedUp == false) {
            ItemPickUp();
        }
        else if(ItemPickedUp == true) {
            ItemDrop();
        }
    }      
}
1 Like

Thank you. I’ve been working for a while at this issue and now I feel a little bit like an idiot for not noticing that.

The longer you look at the same piece of code, the dumber you get until you look at something else. Happens to all of us. :wink:

1 Like