Search Unity

Why should unity properties begin with lowercase?

Discussion in 'Scripting' started by henriquelsm, Mar 20, 2019.

  1. henriquelsm

    henriquelsm

    Joined:
    Jan 3, 2018
    Posts:
    5
    I made the update for Rider 2018, since then I get warnings similar to this one.

    Name 'OnUnLoadSceneEvent' does not match rule 'Unity serialized field'. Suggested name is 'onUnLoadSceneEvent'.

    The warning is displayed only on properties injected by Unity.
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    It is optional convention, as many other conventions.
    Weather naming as Camel Case, or Upper case, or other.
    Generally is good to pick one standard and stick to it, across whole project.

    Just take Unity naming with grain of salt, as sine may seems all over the place.
    Consequence of decade Unity development. ;)
     
    Last edited: Oct 10, 2020
    Bunny83 likes this.
  3. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    856
    If you use Resharper C#, it suggests/recommends _lowerCamelCase naming convention for private fields and UpperCamelCase one for public fields and properties.
    You should Adhere to it. Also, you can change these rules in Resharper setting section or even disable this warning.
    You can check all naming conventions in Resharper->Options->Code Editing->C#->Naming Style
     
    Last edited: Mar 20, 2019
    chadfranklin47 likes this.
  4. BiomotionLab

    BiomotionLab

    Joined:
    Oct 9, 2018
    Posts:
    11
    This doesn't seem to get rid of that warning. There's no option for public serialized unity fields
     
  5. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    856
    I do not understand why you want to change rules for serialized public fields!
    Yes, I think that you can not add a new rule for specific "serialized public fields"
    You can definitely remove warnings using (Alt+Enter) and select or change it.
     
  6. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,998
    I'm reading this as: The new Unity code editor is great. No longer crashing when you type too quickly, and the code-completion finally works all of the time. You no longer have to navigate away and back to enable folding. And when you get frustrated, the warnings will give you a chuckle.

    Does it do that super-annoying thing VS-code does, where you type 1/2 a variable, pause, and it pounces to underline it in red, along with big parts of the code below? Or, gah - does it show named parameters as completion options? Like if you're calling Min(int n1, int n2), it gives you "n1:" as an option? Just the worst.
     
  7. ZombieTFK

    ZombieTFK

    Joined:
    Sep 6, 2016
    Posts:
    55
    Posting a solution as I found the warnings annoying and wanted to share for any travellers coming though.

    Contrary to what is being suggested by mahdii, it is not a c# style convention to use camelCase for public field names or to just accept defaults (Try switching IDEs when you already have hundreds of scripts following a convention the style linter doesn't like by default, or when you join a team that uses their own style guide). In fact, you should not have public fields at all - they should always be encapsulated by properties. There are explicitly no rules for private or protected fields (Which they probably should be - and is what we are talking about here), and none are given by Unity, so use your own preference as long as you are consistent.

    In order to change this, in settings
    Go to Languages & Frameworks -> UnityEngine
    You will see a section under the C# section there, and there is a "style" dropdown menu under Serialized field naming rules, where you can change your serialized field naming convention.
     
    Last edited: Oct 10, 2020
    longsl, PanthenEye and Zaimatsu like this.
  8. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,075
    I was looking up why Rider suggests camelCase by default for public and serialized fields and came upon this thread. It seems it might be a Unity convention? It's definitely not a C# convention, however. So I edited that rule as suggested above.
     
    Last edited: Sep 7, 2021