- Home /
Problem with dropping items from my inventory
I made a simple system for adding items into my inventory, but when I try to remove an object at a certain position, for some reason if it's not the first index in the list, it will transform the one above it into itself, it that makes any sense. This is for picking up items..
static GameObject go;
RaycastHit hit;
public Camera myCamera;
public bool canPickUpItem;
public bool canLootChest;
public Item loot;
public List<GameObject> loots;
void Update(){
if(Input.GetKeyDown(KeyCode.E)){
if(canPickUpItem){
Loot(loot);
}
void Loot(Item item){
SendMessage("AddItem", item);
item.SendMessage("Destroy");
}
void OnGUI(){
if(Physics.Raycast(transform.position, myCamera.transform.forward, out hit, pickUpRange)){
go = hit.collider.gameObject;
if(go.tag == "Item"){
GUI.TextArea(new Rect(Screen.width/2, Screen.height/2, 150, 40), "Press 'E' to pick up '" + go.name + "'");
loot = go.GetComponent<Item>();
canPickUpItem = true;
}
..and this adds them to the inventory:
public List<Item> playerInventory;
Vector2 scrollView;
public bool showInventory;
Rect inventoryRect;
static GameObject model;
public GameObject objThrowSpawn;
void Start(){
playerInventory = new List<Item>();
}
void AddItem(Item it){
playerInventory.Add(it);
Debug.Log("Picked up '" + it.name + "'");
}
void RemoveItem(Item it){
Debug.Log(playerInventory.IndexOf(it));
playerInventory.Remove(it);
}
void OnGUI(){
for(int x = 0; x < playerInventory.Count; x++){
Item index = playerInventory[x];
GUILayout.BeginVertical();
GUILayout.BeginHorizontal();
GUILayout.Label(index.name);
if(index.type == Type.weapon){
if(GUILayout.Button("Equip Left")){
Item curIndex = playerInventory[x];
GameObject equipLeft = (GameObject)Instantiate(Resources.Load(curIndex.name), curIndex.leftEquip.position, curIndex.leftEquip.rotation);
animation.CrossFade(curIndex.name + "EquipLeft");
}
if(GUILayout.Button("Equip Right")){
Item curIndex = playerInventory[x];
GameObject equipRight = (GameObject)Instantiate(Resources.Load(curIndex.name), curIndex.rightEquip.position, curIndex.rightEquip.rotation);
animation.CrossFade(curIndex.name + "EquipRight");
}
}
else if(index.type == Type.armor){
Item curIndex = playerInventory[x];
if(GUILayout.Button("Equip")){
GameObject equipArmor = (GameObject)Instantiate(Resources.Load(curIndex.name), curIndex.armorEquip.position, curIndex.armorEquip.rotation);
animation.CrossFade(curIndex.name + "Equip");
}
}
if(GUILayout.Button("Drop")){
Item curIndex = playerInventory[x];
model = (GameObject)Instantiate(Resources.Load(curIndex.name), objThrowSpawn.transform.position, objThrowSpawn.transform.rotation);
model.name = Resources.Load(curIndex.name).name;
model.rigidbody.AddRelativeForce(new Vector3(0, 30, 60));
RemoveItem(curIndex);
}
else{
GUILayout.EndHorizontal();
GUILayout.Label(index.description);
GUILayout.EndVertical();
}
}
Sorry I know this is a lot of code but I don't know where I went wrong. Please help if you can, thank you.
is it this block of code causing the issue?
if(GUILayout.Button("Drop")){
Item curIndex = playerInventory[x];
model = (GameObject)Instantiate(Resources.Load(curIndex.name), objThrowSpawn.transform.position, objThrowSpawn.transform.rotation);
model.name = Resources.Load(curIndex.name).name;
model.rigidbody.AddRelativeForce(new Vector3(0, 30, 60));
RemoveItem(curIndex);
}
I changed it to a private question. There is no reason for anyone to be able to edit it, so please don't mark it as a community question :)
Your menu system seems to be the culprit (I think?). It looks like you are draw a TON of buttons to the screen. Can we get a screenshot of the inventory system? $$anonymous$$y assumption is that there are multiple "Remove" buttons on top of each other, with the 1 position always at the top of that stack. Therefore, when you click it, it removes the first item. This works if the item you want is the first item.
Of course, this is an assumption until I can see it visually.
Also, let me know if I am misunderstanding your question.