- Home /
Inventory adding items question
Hi,
I've made a small inventory script and it looks like this:
public class InventoryClass : MonoBehaviour
{
public bool isFull = false;
public List<GameObject> inventory = new List<GameObject>();
public GameObject leftHand, rightHand, head, foot, body, legs, gloves, bracelet, ammo;
public void AddItem(GameObject item)
{
if (inventory.Count < 30)
{
inventory.Add(item);
}
else
isFull = true;
}
}
in another script i've made this:
void Update()
{
//raycast detection of objects
cam = Camera.main.transform;
ray = new Ray(cam.position, cam.forward);
//Debug.DrawRay(ray.origin, hit.point, Color.green);
//detect enemy
if (Physics.Raycast(ray, out hit, 30.0f))
{
if (hit.transform.tag == "Enemy")
{// assign the comparison result to is enemy
isEnemy = true;
isItem = false;
isNpc = false;
enemy = hit.collider.gameObject.GetComponent<EnemyClass>();
}
else if (hit.transform.tag == "Item" && hit.distance < 5)
{
isItem = true;
isEnemy = false;
isNpc = false;
item = hit.collider.gameObject.GetComponent<ItemClass>();
//keycodes
if (Input.GetKeyDown(KeyCode.E))
{
playerClass.inventory.Add(hit.collider.gameObject);
Destroy(hit.collider.gameObject);
Debug.Log("pick up");
}
}
}
but the moment i delete the object i can't view the object in my inventory. is there a easy way to add some items to the inventory and delete them from the scenery?
Comment