- Home /
How can I remove an item ffrom a list.
I know that I can remove it with listName.Remove(blah blah) and that would work fine, but I want to remove an item from the button I am holding it in. I have items in a chest and when you click them they go into your inventory, but they also stay in the chest. My problem is that when you click a button to remove that item it just removes the last one in the list and not the one that you click on. Does anybody know the solution to this? If you need to see the script I can post it ,but otherwise please help me find a solution. using UnityEngine; using System.Collections; using System.Collections.Generic;
public class OpenCrateScript : MonoBehaviour
{
static public bool lootCrateOpen = false; //If the loot crate is open or not
private float maxDistance; //Max distance the player can be from the chest to open it.
public Transform player; //The player opening the chest
public GameObject crate; //The crate that we want to delete or spawn at certain times.
public Texture2D CrateWindow;
public Texture2D CloseButton;
public GUIStyle CrateSlotStyle;
public GUIStyle CrateButtons;
public GUIStyle CrateScrollBar;
public GUIStyle Text;
public GUIStyle Empty1;
public static List<Item> lootItems;
private int buttonWidth = 66;
private int buttonHeight = 66;
private int offsetX = 217;
private int offsetY = 62;
private int maxCols = 3;
//LOOT WINDOW SCROLL VIEW
private Vector2 crateWindowScrollView= Vector2.zero;
void Start ()
{
maxDistance = 2.5f;
lootItems = new List<Item> ();
PopulateCrate ();
}
void Update ()
{
if (craftingMenu.craftingMenuOn == true || BuildingMenu.buildingMenuOn == true)
{
lootCrateOpen = false;
}
if (Input.GetKeyUp(KeyCode.E) && lootCrateOpen == false) {
RaycastHit hit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit)) {
if (hit.collider.CompareTag("Loot Crate") && Vector3.Distance(hit.collider.transform.position, player.transform.position) <= maxDistance) {
lootCrateOpen = true;
inventorySystem.inventoryOn = true;
inventorySystem.toolbarOn = true;
}
}
if ( lootCrateOpen == true && Input.GetKeyUp(KeyCode.Tab))
{
inventorySystem.inventoryOn = false;
inventorySystem.toolbarOn = false;
lootCrateOpen = false;
}
}
if (inventorySystem.inventoryOn == true && Input.GetKeyUp (KeyCode.Tab))
{
lootCrateOpen = false;
}
}
void OnGUI()
{
if (lootCrateOpen == true)
{
GUI.BeginGroup(new Rect(701,31,690,800), CrateWindow);
inventorySystem.inventoryOn = true;
inventorySystem.toolbarOn = true;
GUI.Label(new Rect(220,25,100,20),"Small Crate",Text);
if(GUI.Button(new Rect(430,25,15,15),CloseButton,Text))
{
lootCrateOpen = false;
inventorySystem.inventoryOn = false;
inventorySystem.toolbarOn = false;
}
crateWindowScrollView = GUI.BeginScrollView(new Rect(-5,55,450,285), crateWindowScrollView,new Rect(-15,0,42,700));
for(int cnt = 0; cnt < 18; cnt++){
if(cnt < lootItems.Count)
{
if(GUI.Button (new Rect((cnt % maxCols)* buttonWidth + offsetX,cnt / maxCols * buttonHeight + offsetY,buttonWidth,buttonHeight),cnt.ToString (),CrateSlotStyle))
{
AddItem(cnt);
}
}
else
{
GUI.Label (new Rect((cnt % maxCols)* buttonWidth + offsetX,cnt / maxCols * buttonHeight + offsetY,buttonWidth,buttonHeight),string.Empty,CrateSlotStyle);
}
}
GUI.EndScrollView();
GUI.EndGroup();
}
}
private void PopulateCrate()
{
for (int cnt = 0; cnt < 5; cnt++)
{
lootItems.Add (new Item());
}
}
private void AddItem(int cnt)
{
inventorySystem.inventory.Add (lootItems [cnt]);
lootItems.RemoveAt (cnt);
}
}
I think I had a similar situation. What you need to know is the list indexID of the item (you might have a bagPosition or chestPosition which doesn't correspond ot the list indexID).
If that's sounds about right take a look at this http://answers.unity3d.com/questions/629494/c-lists-inventory-logic.html
starting at line 17, the LINQ code to get the list indexID
if you wouldn't $$anonymous$$d to post your code I could take a look at it.
Have you tried the solution I posted under answer to your previous question?
Yes and it doesn't work. It still only removes the last one.
also getyour411 I was confused by the index thing. Could you please explain how I could use it? Here I'll post up the script I'm using
Answer by zharik86 · Mar 29, 2014 at 07:37 AM
The code seemingly correct, and deleting comes from itemloot truly. But the output to the screen, that that remained, in my opinion, incorrectly. After all you have a cycle from 0 to 17. You define that thus there can be a cell as loot from the list (button), or it isn't present (label). When you delete any cell from the list, the quantity of its elements decreases on 1. But the output to the screen will be as though you deleted the last element. You look, you have initially 17 elements and all of them on the screen as buttons. You delete any of them, then you still have 16 elements, but the cycle goes again from 0 to 17 therefore from your code only the last element will be as label. That to correct it, you need to explain to the system, what element was remote, for example:
for(int cnt = 0; cnt < 18; cnt++) {
if(cnt < lootItems.Count) {
if (lootItems[cnt] != null) { // my explain for system of loot
if(GUI.Button (new Rect((cnt % maxCols)* buttonWidth + offsetX,cnt / maxCols * buttonHeight + offsetY,buttonWidth,buttonHeight),cnt.ToString (),CrateSlotStyle)) {
AddItem(cnt);
}
} else {
GUI.Label (new Rect((cnt % maxCols)* buttonWidth + offsetX,cnt / maxCols * buttonHeight + offsetY,buttonWidth,buttonHeight),string.Empty,CrateSlotStyle);
}
}
}
And your AddItem() function:
private void AddItem(int cnt) {
inventorySystem.inventory.Add (lootItems [cnt]);
//lootItems.RemoveAt (cnt);
lootItems[cnt] = null;
}
I hope it to you will help.
The only problem is that the other slots that should be in the chest and show up ad empty aren't there. How could I bring them back to where there are all 18 of them ,but only the 5 have items in it?
@NutellaDaddy If I correctly understood, you want in addition to relocation in inventory, to make relocation back of inventory. Then it is necessary to move to the first free slot. For example:
public void relocation(int numIt) {
bool addingList = true; //Just in case, and that suddenly you will exceed quantity of cells of the list loot
for(int i=0; i <lootItems.Count; i++) {
if (lootItems[i] == null) {
addingList = false;
lootItems[i] = ...//item, from your inventory behind number numIt
break;
}
}
if(addingItem) {
lootItems.Add(...) //item, from inventory behind number numIt
}
}
Your answer
Follow this Question
Related Questions
Removing objects from a list in C# 2 Answers
Add random amount of random items from one list to another? 2 Answers
Problem with removing an item from a list 0 Answers
Unknown remaining objects after removing and destroying from list and parent game objects 0 Answers
How to ignore base class? 1 Answer