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 TobiasJohansson · Mar 31, 2014 at 09:15 AM · listprogrammingarraysinventory

Dynamic update List<>

Like the title says. Is it possible to update a List<> dynamically?

I currently use a List<> for my player inventory, however when the player have used an item and I remove that item it removes every item before it in the list as well. I assume this is since the list can't update the items before to new positions.

Second question: Should I use an array for this instead?

The thing is that I have a player inventory of 9 objects, and at the moment I never want it to get bigger, so I guess I could use an array. But I think list is better and easier since I would not then have to check how many items the player currently has all the time since I just go through how big the list currently is to check that. But then as I said I need to update it dynamically when the player uses an item.

So how do people usually do here?

 // Code that adds item to inventory                    
 if (this.GetComponent<pl_Inventory>().iCurrItemsInInv <= this.GetComponent<pl_Inventory>().iMaxItemsInInv)
                     {
                         this.GetComponent<pl_Inventory>().listOfItems.Add(rayHit.collider.gameObject.GetComponent<Item_Base>());
                         rayHit.collider.gameObject.transform.parent = gHand.transform;
                         rayHit.collider.gameObject.SetActive(false);
                     }
                     else
                     {
                         Debug.Log("Inventory is full");
                     }

 // Code that removes the item

 /// <summary>
 /// Here we get all the puzzle gameobjects. There will be turned off when the player if finished, or even deleted.  
 
 /// *This should be the prefab with all puzzle parts in it. Going to be deleted or turned off when done. 
 /// </summary>
 public GameObject clItemNeeded;

     
 if (coll.tag == "Player")
         {
             gFinishedStairCase.gameObject.SetActive(true);
             gPlayer.GetComponent<pl_Inventory>().listOfItems.RemoveAt(clItemNeeded.GetComponent<Item_StairPlanks>().iItemID);
             Destroy(clItemNeeded);
         }
Comment
Add comment · Show 4
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 whydoidoit · Mar 31, 2014 at 09:16 AM 0
Share

There's something wrong with your code. Lists can remove any single item without affecting the rest. Post your code.

avatar image TobiasJohansson · Mar 31, 2014 at 10:40 AM 0
Share

Added code for removing the object.

avatar image CodeElemental · Mar 31, 2014 at 10:50 AM 0
Share

is iItemID property an actual index of the item in list ?

Because gPlayer.GetComponent().listOfItems.RemoveAt() requires an index.

avatar image whydoidoit · Mar 31, 2014 at 11:39 AM 0
Share

You can just use Remove with clItemNeeded.GetComponent() no need to get the index and use RemoveAt

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by TobiasJohansson · Mar 31, 2014 at 11:28 AM

Okay, added this to the code, now it seems to work:

         if (coll.tag == "Player")
         {
             // Get the index for the item we want to remove
             int iRemovableItem;
             iRemovableItem = gPlayer.GetComponent<pl_Inventory>().listOfItems.IndexOf(clItemNeeded.GetComponent<Item_StairPlanks>());
 
             // Turn on puzzle piece
             gFinishedStairCase.gameObject.SetActive(true);
 
             // Set current item to -0 and bHoldingItem to false so player no longer has any item in his hands. 
             gPlayer.GetComponent<pl_Inventory>().bHoldingItem = false;
             gPlayer.GetComponent<pl_Inventory>().iChosenItem = -1;
 
             // Remove item from list and destory item so player no longer has it. 
             gPlayer.GetComponent<pl_Inventory>().listOfItems.RemoveAt(iRemovableItem);
             Destroy(clItemNeeded);
         }
Comment
Add comment · Show 3 · 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
avatar image gjf · Mar 31, 2014 at 01:19 PM 0
Share

you could make this more efficient (and more maintainable) by storing the reference to the item. also, it's probably better to store the Item_StairPlank component reference somewhere too.

 if (coll.tag == "Player")
 {
     // Get reference to item to remove
     var item = gPlayer.GetComponent<pl_Inventory>();

     // Get the index for the item we want to remove
     int iRemovableItem = item.listOfItems.IndexOf(clItemNeeded.GetComponent<Item_StairPlanks>());
 
     // Turn on puzzle piece
     gFinishedStairCase.gameObject.SetActive(true);
 
     // Set current item to -0 and bHoldingItem to false so player no longer has any item in his hands. 
     item.bHoldingItem = false;
     item.iChosenItem = -1;    // Comment says -0...
 
     // Remove item from list and destory item so player no longer has it. 
     item.listOfItems.RemoveAt(iRemovableItem);
     Destroy(clItemNeeded);
 }
avatar image TobiasJohansson · Mar 31, 2014 at 04:16 PM 0
Share

Could you maybe explain what you mean?

avatar image gjf · Apr 01, 2014 at 08:39 AM 0
Share

which bit?

depending on the overall code structure, it might be more efficient to store the reference to the Item_StairPlanks component in a public/private variable for the code here to use ins$$anonymous$$d of calling GetComponent each time the player collides with it. if you use it in other places, it might make sense - especially if the code is cleaner/simpler.

without seeing the entire codebase, it's impossible to say whether that's worth doing or not...

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

A node in a childnode? 1 Answer

[C#]Inventory script help. 3 Answers

Adding Item object to Inventory List 0 Answers

Can someone help me with an inventory? 1 Answer


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