0% found this document useful (0 votes)
3 views1 page

Pick Up Item

The script defines a PickUpItem class in Unity that allows a player to pick up items within a certain distance when the 'E' key is pressed. It checks if the item already exists in the player's inventory and if there is space to add the new item. If the item is successfully added, the script destroys the item game object in the scene.

Uploaded by

drgvfmjmrr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Pick Up Item

The script defines a PickUpItem class in Unity that allows a player to pick up items within a certain distance when the 'E' key is pressed. It checks if the item already exists in the player's inventory and if there is space to add the new item. If the item is successfully added, the script destroys the item game object in the scene.

Uploaded by

drgvfmjmrr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

using UnityEngine;

using System.Collections;
public class PickUpItem : MonoBehaviour
{
public Item item;
private Inventory _inventory;
private GameObject _player;
// Use this for initialization

void Start()
{
_player = GameObject.FindGameObjectWithTag("Player");
if (_player != null)
_inventory =
_player.GetComponent<PlayerInventory>().inventory.GetComponent<Inventory>();
}

// Update is called once per frame


void Update()
{
if (_inventory != null && Input.GetKeyDown(KeyCode.E))
{
float distance = Vector3.Distance(this.gameObject.transform.position,
_player.transform.position);

if (distance <= 3)
{
bool check = _inventory.checkIfItemAllreadyExist(item.itemID, item.itemValue);
if (check)
Destroy(this.gameObject);
else if (_inventory.ItemsInInventory.Count < (_inventory.width * _inventory.height))
{
_inventory.addItemToInventory(item.itemID, item.itemValue);
_inventory.updateItemList();
_inventory.stackableSettings();
Destroy(this.gameObject);
}

}
}
}

You might also like