- Home /
Problem with picking up objects using a raycast and placing them in the inventory.
Hi there,
So I made two scripts both attached to a different gameobject. I tried to get an input from the user and then draw a line to the item. If the item would be in a certain range i'd add the item to the inventory and then destroy the item from the world so it would be placed inside my inventory.
The problem is that whenever I walk up to an item and pick it up every other item is automaticlly also picked up. But I only want it to pick up that one item I was looking at.
This is my script:
#pragma strict
private var _inventoryScript : Inventory;
private var _player : GameObject;
var maxDistance : float = 2;
function Start()
{
_player = GameObject.FindGameObjectWithTag("Player");
_inventoryScript = _player.GetComponent(Inventory);
}
function Update()
{
if(Input.GetKeyDown(KeyCode.E))
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width * 0.5, Screen.height * 0.5, 0));
if(Physics.Raycast(ray, hit, maxDistance))
{
if(hit.collider.gameObject.tag == "Item")
{
_inventoryScript.AddItem(1);
Destroy(gameObject);
}
}
}
}
This script is the same for every item all I did was change the number in the AddItem function when i'm calling it to a certain item ID.
How can I make it so that when I try to pick up the item it doesn't place all the other items in my inventory aswell?
Answer by robertbu · Oct 26, 2014 at 09:22 PM
Assuming this is on every item, you can change line 23 to:
if (hit.transform == transform)
Note it would be better to only have this script on one game object. Then rather than the change I just outlined, you could change line 26 instead:
Destroy(hit.transform.gameObject);
And therefore you would only have a single Raycast() rather than a Raycast() by every item in the scene.
If i'd put it on one gameobject then how would I tell my _inventoryScript.AddItem function which item it should add to my inventory? I tried this before but I couldn't get it to work.
Right now you are doing:
_inventoryScript.AddItem(1);
So can I assume that '1' is the number for this specific item? So ins$$anonymous$$d you would have a script on each item (or you could use a tag or name), that indicates the item number.
public var itemNo = 1;
You would set the item number in the inspector. A enum would be better. And this could would not have Raycast() code. Let's assume the script on the items is called 'Item'. Then the Raycasting code (which exists once and on an empty game object and not an item) would be something like:
function Update()
{
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width * 0.5, Screen.height * 0.5, 0));
if(Physics.Raycast(ray, hit, maxDistance))
{
if(hit.collider.gameObject.tag == "Item")
{
int itemNo = hit.collider.GetComponent(Item).itemNo;
_inventoryScript.AddItem(itemNo);
Destroy(hit.transform.gameObject);
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613170009im_/https://answers.unity.com/themes/thub/images/avi.jpg)