Raycast 'picking up' items issue

The problem I’m facing is…
Periodically when I ‘pick up’ a gameobject, and other objects i can also ‘pick up’ are quite close to each other, it will pick up both items up, even though the raycast is only hitting one at a time.

```csharp
**using UnityEngine;
using System.Collections;

public class PickUp : MonoBehaviour {

public LayerMask interaction;
public bool canPickup; // keep public for testing
public Inventory inv; // keep public for testing

private Item item;

/// Need to fix up picking up objects (believe raycast isnt working properly)
/// Within this block of code
private void Update () {
    if (canPickup && inv != null) {
        Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit h; // Using for testing purposes
        if (Physics.Raycast(r, out h, 100f, interaction, QueryTriggerInteraction.Ignore)) {
            Debug.DrawLine(r.origin, r.GetPoint(100), Color.blue);
            Debug.Log(h.transform.name);
            if (Input.GetKeyDown(KeyCode.E)) {
                inv.pickUpObject(SwitchState());
            }
        }
    }
}

// Add a type for each class inherited from Item
private Item SwitchState () {
    System.Type t = GetComponent(gameObject.tag).GetType();
    Item i = null;
    // Type Checking
    // Type Gun
    if (t == typeof(Gun)) {
        i = gameObject.GetComponent<Gun>();
    }
    // Type Sword
    else if (t == typeof(Sword)){
        i = gameObject.GetComponent<Sword>();
    }
    return i;
}

private void OnTriggerEnter (Collider other) {
    if (other.CompareTag("Player")) {
        canPickup = true;
        inv = other.GetComponent<Inventory>();
    }
}

private void OnTriggerExit (Collider other) {
    if (other.CompareTag("Player")) {
        canPickup = false;
        inv = null;
    }
}

}**
```
An image of the object setup…

Your problem is that every item is ray casting and picking up - that logic should be owned by the player and only run once. Right now, as long as the ray cast hits any item, every item within range will be picked up.

Additionally, you don’t need the switchstate method. Just getcomponent on the base class, item, and it will grab the proper inherited class of gun or sword.

1 Like

@Zaladur thanks for replying,

I see what you mean, I’m rewriting my code to a similar idea of what you mentioned, thanks!
Also the code above didn’t ‘pick up’ both times 90% of the time, even though they were both in range of the player.
I worked around it by comparing if the transform of the hit was equal to the transform of that specific object (which probably isnt the best way), but I’m rewriting the code anyway.

Thanks for the help again!
Especially with the getcomponent functionality, that helped heaps!
(Maybe, that it grabs the inherited class if the bass class is passed to it, should be included in the documentation?)

One way to look at it is that every Gun and Sword is an Item, so if you ask for an Item, they are legitimate targets for the request. If I asked you for a piece of fruit and you had an apple, you would give it to me, even though I didn’t specifically ask for an apple. This is the entire reason behind using polymorphism - you can treat everything like the base class and everything will work out fine. Its only when you are using specific methods found in children that you have to be more specific.