Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Philip_Smyth · Jun 05, 2013 at 09:10 PM · c#objectlistclassremove

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
Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mr-JWolf809 · Jun 05, 2013 at 09:46 PM 0
Share

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?

avatar image Philip_Smyth · Jun 05, 2013 at 10:02 PM 0
Share

i dont think you are missing anything. I just dont understand why it isnt happening, a little annoying but thanks for trying :)

avatar image Mr-JWolf809 · Jun 05, 2013 at 10:21 PM 0
Share

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 :)

avatar image Philip_Smyth · Jun 05, 2013 at 10:46 PM 0
Share

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

avatar image Philip_Smyth · Jun 05, 2013 at 10:49 PM 0
Share

btw i tried editing it, comment out line 102 on inventory.cs

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Project can be found here!

I Opped the comment to an answer. And you have a nice day too.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Removing objects from a list in C# 2 Answers

Can I make a list of hashtables or classes with pragma strict 2 Answers

Instantiating objects from a class? (C#) 1 Answer

Creating instance of class that does not extend MonoBehaviour 1 Answer

How can I make a list of Classes or Scripts? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges