gui button detection

I’m having a strange problem with my script and can’t figure out why. When I click a button it drops the appropriate object but always remove the highest up button from the GUI. Any possible solutions would be awesome, tia! :slight_smile:

for(int i = 0; i < inventory.Count; i++) {
    if(GUILayout.Button(inventory[i].item_name)) {
        DropItem(inventory[i]);
    }
}
public bool DropItem(Item newItem) {
    GameObject player = GameObject.Find ("Player");
    GameObject instance = (GameObject)Instantiate(Resources.Load(newItem.item_name), player.transform.position + player.transform.forward + Vector3.up* 0.5f, player.transform.rotation);
    instance.name = newItem.item_name;
    inventory.Remove(newItem);
    return true;
}

Try just passing the index location and do a RemoveAt instead of remove object…

for(int i = 0; i < inventory.Count; i++) {
    if(GUILayout.Button(inventory[i].item_name)) {
        DropItem(i);
    }
}
public bool DropItem(int index) {
    GameObject player = GameObject.Find ("Player");
    Item newItem = Inventory[index];
    GameObject instance = (GameObject)Instantiate(Resources.Load(newItem.item_name), player.transform.position + player.transform.forward + Vector3.up* 0.5f, player.transform.rotation);
    instance.name = newItem.item_name;
    inventory.RemoveAt(i)
    return true;
}

Wow Eiznek, thank you so much, I’ve been trying to fix this bug for days!!