Home > Software design >  Unity should I write first the Serialized Fields variables or the Public variables
Unity should I write first the Serialized Fields variables or the Public variables

Time:06-18

I was wondering if in the order of a class should I write first the Serializedfileds that let me edit them in the editor, or the public variables (that even can be nonSerialized) but can be edited in the editor too.

I use to do it in this way:

-Public Vars -Serialized vars -Private Vars -Public Methods _Private Methods

It's that right?

CodePudding user response:

From a functional point of view, it doesn't matter. You can order them however you want, it will still work the same.

However, there are coding guidelines most developers adhere to. Jonathan Wright answered a similar question where he also linked the c# coding guidelines he cited from:

You would order them as follows:

  • Fields
  • Constructors
  • [...]
  • Properties
  • [...]
  • Methods

Within each of these groups order by access: (SA1202)

  • public
  • internal
  • protected internal
  • protected
  • private

So while members with attributes like [SerializeField] are not explicitly listed, I would order them the same way you do, since the [SerializeField] attribute exposes the field to the Unity Editor and makes them accessible from outside.

CodePudding user response:

It does not matter which order you choose, so long as you are consistent and have some sort of structure that you follow

  • Related