- Home /
Removing a class object from a list(.remove not working) C#
Hello, I have been working on this inventory system for my game and it is mostly complete apart from one problem. The code below shows the GUI and workings of the inventory. When an item in the inventory is clicked it will appear in the currently equipped box. However the item will not be removed from actual inventory (backpack if you will). Below I will give the code for the Inventory and the Item class, which is the class used to create the list of objects.
(p.s. This was converted from a javascript tutorial on making an inventory, if that has caused the problem please let me know.)
Since you may not see it after this code, I just want to say thank you for your help and have a nice day :)
Sorry the code is a little messy, tried to make it presentable as best i could
Inventory.cs
/* Inventory.cs coded by Philip Smyth 21/05/2013. For use in commercial products coded in C#.
* Will need to be edited to adjust with ItemClass.cs and AddItem.cs editing done.
*
*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic; //To deal with lists
public class Inventory : MonoBehaviour {
public bool equipped; // Will say if there is something equipped
public ItemClass equippedItem; // The item that is equipped
public Transform playerHand; // the hand of the player, so the item can be parented to it for animations and such
public List<ItemClass> playerInventory = new List<ItemClass>(); // List of ItemClass
public Vector2 scrollView; // Will allow us to use scroll bar.
private int appear;
// Use this for initialization
void Start () {
appear = 0;
}
// Update is called once per frame
void Update ()
{
if(this.equippedItem.itemType.Equals(ItemClass.ItemType.Weapon)) //Check if there is something equipped or not.
this.equipped = true; // If true then bool equipped will be set to true
else
this.equipped = false; // If false then bool equipped will be set to false.
if(Input.GetKeyDown(KeyCode.P)) //This and if statement below used to use OnGui only when pause menu is
{appear = appear + 1;} //active on another script
if(appear == 2)
{appear = 0;}
}
void OnGUI()
{
//Inventory Display Management.
if(appear == 1){
GUILayout.BeginArea(new Rect( // Will begin the GUI Area. In the top left corner
(float) (Screen.width-500), // X
(float)0, // Y
(float)300, // Width
(float)300)); // Height
GUILayoutOption[]optionArray1 = new GUILayoutOption[] {GUILayout.Width((float)500), GUILayout.Height((float)500)};
this.scrollView = GUILayout.BeginScrollView(this.scrollView, optionArray1); // Start scrol view and assign it to the scrollView variable
for(int i = 0; i < this.playerInventory.Count; i++) // ran for every item in the inventory
{
GUILayout.BeginHorizontal(new GUILayoutOption[0]); // begin the horizaontal area
if(GUILayout.Button(this.playerInventory[i].icon, new GUILayoutOption[0]) // If the button is clicked...
&&(this.playerInventory[i].itemType.Equals(ItemClass.ItemType.Weapon)) // ... and the item is represented is a weapon...
&& !this.equipped) // ... and there is nothing currently equipped.
{
this.equippedItem = this.playerInventory[i]; // assign the inventory item to equippedItem
Transform thing; //used for the instantiation
thing = (Transform)Object.Instantiate(this.playerInventory[i].itemPrefab, // Instantiate the prefab of the weapon
this.playerHand.position, // and maintain the prefabs rotation
Quaternion.identity);
thing.parent = this.playerHand; // set the parent to the player hand(for animations etc.)
this.playerInventory.RemoveAt(i); // Remove the item from the inventory *******THIS IS WHERE I THINK ITS WRONG********
// dont need to continue processing item, so jump back to beginning of function.
return;
}
GUILayout.Box(this.playerInventory[i].name, new GUILayoutOption[0]); //If not clicked yet, display name
GUILayout.EndHorizontal(); // End the horizontal area
GUILayout.Box(this.playerInventory[i].description, new GUILayoutOption[0]); // Display the description.
}
GUILayout.EndScrollView(); // End the scroll view
GUILayout.EndArea(); // End the area.
// Weapon Slot Display
if(GUI.Button(new Rect(
(float)((30)), // X
(float)(Screen.height - 70), // Y
(float) 50, // Width
(float) 50), // Height
this.equippedItem.icon) // Item clicked an icon
&& this.equippedItem.itemType == ItemClass.ItemType.Weapon){ // If clicked and a weapon
for(int x = 0;x < 1; x++ ){
this.playerInventory.Add(this.equippedItem); // Add item to inventory
}
//Object.Destroy(GameObject.FindGameObjectWithTag("Weapon")); // Destroy instantiated prefab WASNT NEEDED
this.equippedItem = new ItemClass(); // replace with blank item
}
}
}
}
//End
Then the ItemClass Scrips ItemClass.cs
/* ItemClass.cs coded by Philip Smyth 21/05/2013. For use in Commercial products coded in C#.
* This is not applied to a game object however it is called by both Inventory.cs and AddItem.cs
* Little editing to be required for this script. However further infor can be included.
*/
using UnityEngine;
using System.Collections;
[System.Serializable]
public class ItemClass{ // make public to be accessed by other scripts
//Feilds
public string name; // The name of the item
public string description; // Describe the item
public Texture2D icon; // The 2D icon that will be used in the inventory and hotbar.
public int id; // ID of the item for future editing
public Transform itemPrefab; // The 3D model of the item.
public ItemType itemType; // The type of the item (weapon, armour, other etc.)
public int dmg;
//Nested Types
public enum ItemType // The available item types to be chosen.
{
None,
Weapon,
Other,
Armour
}
}
//End
This seems to work for me. As long the item I click on is ItemClass.ItemType.Weapon everything works fine.
Is there Something I am missing?
i dont think you are missing anything. I just dont understand why it isnt happening, a little annoying but thanks for trying :)
If you keep having this problem, you could try and create a small unity project that shows the problem and upload it here. It would make it easiere for others to test :)
ok here you go, when equipped it should remove it from the player inventory (top right) and then place it in the equip box (bottom left) it will equip but the item remains in the inventory.meaning that when it is unequiped, two of the same item appear in the inventory.
Sorry had to use mediadire to give the scripts and scene
http://www.mediafire.com/download/idhbkngzgw3f6ku/problem_show.unity
http://www.mediafire.com/download/aa4qqxusg8crmlk/Inventory.cs
http://www.mediafire.com/download/de4sd3vpd8391nj/ItemClass.cs
btw i tried editing it, comment out line 102 on inventory.cs
Answer by Mr-JWolf809 · Jun 06, 2013 at 01:20 AM
I still cannot reproduce the problem. I did have a problem destroying the created prefrab when doing the unequip. Fixed it by keeping a reference in the ItemClass:
[HideInInspector]
public Transform VisualItem; // The created itemPrefab
But the item is getting equiped when clicking on the large icon (and removed from the Player Inventory list). And it is getting unequiped when clicking on the small (and added to the Player Inventory list)
Try this project and tell me if the item (claw in this case) is not getting equiped/unequiped
I Opped the comment to an answer. And you have a nice day too.