# Update if-statements activate wrong method.

**URL:** https://discussions.unity.com/t/update-if-statements-activate-wrong-method/664885
**Category:** Scripting
**Tags:** Scripting
**Created:** [May 16, 2017, 6:09pm UTC](https://discussions.unity.com/t/update-if-statements-activate-wrong-method/664885 "2017-05-16T18:09:36Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![RVDL\_IT](https://avatars.discourse-cdn.com/v4/letter/r/bcef8e/32.png) [@RVDL\_IT](https://discussions.unity.com/u/RVDL_IT)
#### Post date: [May 16, 2017, 6:09pm UTC](https://discussions.unity.com/t/update-if-statements-activate-wrong-method/664885/1 "2017-05-16T18:09:36Z")

</div>

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.

```csharp
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;
            }
        }  
    }
}

```

---

<div class="post-metadata">

### Author: ![Baste](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/baste/32/880289_2.png) [@Baste](https://discussions.unity.com/u/Baste)
#### Post date: [May 16, 2017, 6:26pm UTC](https://discussions.unity.com/t/update-if-statements-activate-wrong-method/664885/2 "2017-05-16T18:26:03Z")

</div>

> [@RVDL\_IT](#):
>
> ```auto
> void Update() {
> if (Input.GetKeyDown("e")) {
> if(ItemPickedUp == false) {
> ItemPickUp();
> }
> if(ItemPickedUp == true){
> ItemDrop();
> }
> }      
> }
> 
> ```

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:

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

```

---

<div class="post-metadata">

### Author: ![RVDL\_IT](https://avatars.discourse-cdn.com/v4/letter/r/bcef8e/32.png) [@RVDL\_IT](https://discussions.unity.com/u/RVDL_IT)
#### Post date: [May 16, 2017, 6:36pm UTC](https://discussions.unity.com/t/update-if-statements-activate-wrong-method/664885/3 "2017-05-16T18:36:21Z")

</div>

> [@Baste](#):
>
> 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:
> 
> ```auto
> void Update() {
> if (Input.GetKeyDown("e")) {
> if(ItemPickedUp == false) {
> ItemPickUp();
> }
> else if(ItemPickedUp == true) {
> ItemDrop();
> }
> }     
> }
> 
> ```

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.

---

<div class="post-metadata">

### Author: ![StarManta](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/starmanta/32/280327_2.png) [@StarManta](https://discussions.unity.com/u/StarManta)
#### Post date: [May 16, 2017, 6:41pm UTC](https://discussions.unity.com/t/update-if-statements-activate-wrong-method/664885/4 "2017-05-16T18:41:38Z")

</div>

> [@RVDL\_IT](#):
>
> 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. 😉
