• 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
6
Question by canahari · Dec 28, 2011 at 11:24 PM · serialization

[NonSerialized] vs [HideInInspector] question

Hi! What does serialization mean? I mean I know what serialization is, but I don't know what Unity uses it for. I would think that it is for variables to show up or not to shop up in the editor, but for that there is the HideInInspector attribute. I only would like to know what is the difference between NonSerialized and HideInInspector. :)

Thanks in advance.

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
14
Best Answer

Answer by by0log1c · Dec 29, 2011 at 03:02 AM

Each variable can be either shown or hide in the inspector. Each variable can be either saved between sessions, or not. Add to that the normal variable scope private/public.

By default, Unity use the same logic for each variable; private are NonSerialized and HideInInspector; public are SerializeField (and shown in inspector).

  • HideInInspector make sure a variable is not displayed.
  • NonSerialized make sure a variable state is reset to default on game state change.
  • SerializeField make sure a variable value instance has its own default value.

You can add these attributes on both private and public, where it logically applies. I'd add to that to be aware of HideInInspector [SerializeField] public variables, because they could hold value different of what is displayed in the code, and without removing the HideInInspector attribute - and possibly having to check/reset every instance of the code, you'll never know.

EDIT: Create a new script with only the code below, in JS for simplicity, drag it twice(2x) on a GameObject, try each manipulation listed below then hit play and see what happens:

public var myTest:String = "default";

function Start():void { Debug.Log(myTest); }

  • Hit Play. Both should print "default".
  • In the inspector, change one of them to "anything". One should print "default" the other should print "anything".
  • In the code, add the @HideInInspector attribute. One should print "default" the other should print "anything". None are visible and the code let think both hold the "default" value. This is what I'm warning about.
  • In the code, add the NonSerialized attribute. Both print "default".

The HideInInspector attribute make it invisible in the inspector but still allow each instance of a SerializeField to hold its own default value. NonSerialized force even a public field to reset to the code default value when you enter and leave play mode.

Its all a game of variable scope/default value/inspector visibility.

Comment
Add comment · Show 5 · 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 canahari · Dec 29, 2011 at 09:27 AM 0
Share

What is a game state change? When for example the game is started again? [SerializeField] variables keep their value then? I mean if I have monsters in my game, will they start with the same hit points and from the same position where they were when the player exited?

avatar image by0log1c · Dec 29, 2011 at 12:55 PM 0
Share

I've edited my question to elaborate and give an example. When you enter OR leave play mode. Unity try to reset the scene and for each object instance, it looks for a Serialized value, if it has one, it use that, otherwise it refer to the default constructor (what's in the code). So SerializeField equals to "can be modified in editor mode".

avatar image canahari · Dec 29, 2011 at 07:39 PM 0
Share

Thank you very much for the answer :)

avatar image slippdouglas · Feb 16, 2014 at 09:24 AM 0
Share

It appears that NonSerialized also forces the property to be hidden in the inspector.  It would seem Unity's inspector can't show anything that's not being serialized.

avatar image whydoidoit · Feb 16, 2014 at 09:27 AM 0
Share

Yeah the inspector is a visualisation of the serialized state of the object.

avatar image
6

Answer by Jessy · Dec 29, 2011 at 02:36 AM

HideInInspector keeps your variables saved, but removes clutter from the Inspector. I use it all the time, in combination with [SerializeField]. NonSerialized is used for public variables, which are the only ones that are serialized by default. If you don't need them saved, then NonSerialized is a more appropriate choice than HideInInspector, if only for semantic reasons.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

How would I go about storing 3D object data into a database? 5 Answers

Storing data about an asset only in the editor 2 Answers

Unity types unable to be serialized 2 Answers

Serialization 2 Answers

Why do we need to check BitStream.isWriting when serializing? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges