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 Mincemeat · Jan 22, 2015 at 01:38 PM · inventory system

[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.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · 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 Mincemeat · Jan 22, 2015 at 09:56 PM 0
Share

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.

avatar image
0

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.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Array index is out of range? 1 Answer

How to go around creating inventory with new GUI (4.6 Beta)? 1 Answer

unity3d simple inventory 1 Answer

Make a event trigger raycast ignore a layer 1 Answer

Struggling with part of my crafting-algorithm and don't know what my mistake is 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