- Home /
[Answered]Upon Destroying GameObject Changes Made To A List Are Discarded And Reverted.
This is kind of complicated to ask in question form so I'm going to try my best.
I am creating an inventory system in which you can click two items and have the two items swap places in the player's inventory. I'm using the new UI system for this, not OnGUI.
The following code handles the player's input for opening the inventory:
Code Removed.
The above code is attached to the entity that owns the inventory (This is always a player as it's a single player game and there are no other gameobjects using the tag "Player").
When the I button is pressed It creates a new canvas with a panel attached to it. The pannel has the following script attached:
Code Removed
The above script creates a number of slot gameobjects equal to the number of items in the player's inventory. It then parents them to the inventory panel (As the inventory panel utilizes the auto grid feature), and assigns them an index and a parent panel value as well as the sprite they should display.
The following code is the code attached to each individual slot. It is also where the majority of the code that actually DOES things is. I believe this is where the error is but I've looked at it over a period of a couple of days with no luck.
Slot Code:
Code removed.
The above code utilizes the on pointer down interface that comes with Unity's new UI system. Basically when i press down on one of the slots it will store the information of that slot inside of the panel's InventoryUIHandler script. It does this when I select my first Item and then when i select my second Item. It then swaps these two items and changes the graphics being displayed in the UI.
The code works perfectly until I close the UI window for the inventory. When the gameobject is destroyed it reverts the player's inventory back to what it was before all of the changes took place. I have also tried disabling the inventory window instead of destroying it and maintaining it persistently but the same bug occurred and reverted the inventory of the player when the inventory UI was disabled.
I can't for the life of me figure this out... It's likely something simple such as forgetting my c# basics but like I said I have no idea. Any help is appreciated and sorry for the VERY long question haha.
EntityHandler script if you need it:
Code Removed.
Once again thank you for any help.
Answer by theultimatepwn · Jan 22, 2015 at 03:57 PM
In your code it appears the array is created fresh every time you press "i". A common method is just to have it disabled or invisible when not in use. destroying it does just as one would expect, it removes the game object, all attached components, and children from the instance so they can be removed from memory.
I assume you mean the String array. That's for debugging purposes so I can see the item names. The actual inventory is the List that is inside of the EntityHandler. The array is supposed to refresh and resize every time you press I so it can hold any new items that appear if you pick something up. I know it doesn't work because that array sets the strings based on the index of each item. It gets it's information solely from the List.
If you typo'd and meant to say List. I did actually try the disable / enable method with the same results.
Answer by Mincemeat · Jan 23, 2015 at 02:15 AM
Solved it... It was as suspected something REALLY stupid. I had the indexes reversed so it cancelled out all of the changes I was making.
inventory[indexOfItemOne].itemInventoryIndex = indexOfItemTwo;
inventory[indexOfItemTwo].itemInventoryIndex = indexOfItemOne;
was supposed to be:
inventory[indexOfItemOne].itemInventoryIndex = indexOfItemOne;
inventory[indexOfItemTwo].itemInventoryIndex = indexOfItemTwo;
I am indeed a moron haha.