Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 malkere · Jun 30, 2017 at 02:29 PM · scripting problemruntimemonobehaviour

Selecting GameObject in hierarchy during runtime alters variables

Short story: In runtime, if ever I select my player in the hierarchy all of the null InventorySlot.Item class objects in my player inventory get magically initialized no longer returning null.

Long story: Clicking on the player GameObject causes the problem... that right there has me stumbled. The list in question:

 [HideInInspector] public List<InventorySlot> equipmentSlot;

is quite simply a list of a class that stores some enums and an itemAmount int, nothing fancy. I have checked all references to it project wide, I have added Debugs into everything that goes anywhere near it, and ONLY when I click on the player GameObject in the hierarchy does the problem occur.

The problem: when the game starts the List is initialized setting the Item variable in each InventorySlot to null, because there are no items in our inventory yet. I can confirm that with Debugs in runtime, they are null. Then I click on the player GameObject in the hierarchy, and call the same Debug, now the Item is not null and returns as an initialized Item, but of course all of it's variables are empty and now the game doesn't know what to do with it.

How does that make any sense?? Monobehaviors respond to in editor selection some way that I don't know about?

The Count of the List remains unchanged. The order of the list remains unchanged. The InventorySlots that have things in them, or in other words are not null, go unaffected. The InventorySlots that are null somehow are magically assigned an empty Item.

The InventorySlot class doesn't even default to an initialized Item, it loads them as null:

 public class InventorySlot {
 
     public int slotNumber = -1;
     public int itemAmount = 0;
     public Item item = null;
     public InventorySlotTypes slotType = InventorySlotTypes.Null;
 
 }

I can pause, click on everything else in the scene, unpause no problems. Pause, click on the player, deselect the player, unpause and the inventory is broken. By broken I mean empty slots are supposed show their Item as null, but now they are all initialized somehow.

Would never happen to a player in a real build sure, ya I could hack around it to find empty items and reset them as null, but what in the heck is going on??

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

1 Reply

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

Answer by hexagonius · Jun 30, 2017 at 02:47 PM

There's only one reason I can think of this is happening. When writing custom inspectors, whenever a gameobject is selected, a MonoBehaviours serialized values get read and written to, depending on what code is used. maybe the same is happening for classes that do not have a custom inspector. List<InventorySlot> is public, therefore serialized for the inspector. For the serialization, null does not exist and an actual instance information is saved. Maybe, without using a custom inspector, the default is just that, write the serialized values to the actual gameobject.
You should be able to check that by simply adding the NonSerialized attribute in addition to the HideInInspector attribute, like so [HideInInspector, NonSerialized]

Comment
Add comment · Show 2 · 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 malkere · Jun 30, 2017 at 02:59 PM 0
Share

Thank you @hexagonius NonSerialized did in fact prevent the problem Haven't 100% wrapped my head around the answer and all possible scenarios, but your explanation makes sense. Thank you for the enlightenment.

avatar image malkere · Jul 01, 2017 at 12:20 AM 0
Share

To word it a little different as I understood it: Calling on the inspector preview of the script was the cause which happens when the inspector tab is visible and the gameobject with the script attached to it is selected. This attempts to initialize any public variables as per serialization is required to show them in the inspector. I was using the HideInInspector attribute to hide the public List, but it was still being processed by the inspector. Adding NonSerialized stopped the initialization behavior. This also has not interrupted my write to disk functions which I take to mean the List is still serializable, it's just not automatically serialized unless explicitly done.

Started happening a week ago sporadically I had no idea what was causing it!! Glad it's off my plate! Thanks again.

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

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

Related Questions

If-else statement executing incorrectly in foreach loop,why? 0 Answers

WaitForSeconds() and WaitForSecondsRealtime() negative number concern!!!! 4 Answers

How to store data in script and attach it later? 0 Answers

How to run script in editor 1 Answer

What is the best way to update array in runtime? 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