Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Visual studio adds "private" to standart unity methods

Discussion in 'Code Editors & IDEs' started by Andrew3371, Apr 19, 2023.

  1. Andrew3371

    Andrew3371

    Joined:
    Mar 27, 2023
    Posts:
    23
    If I create a new Monobehaviour script it contains "void Start()" by default.
    But when I add this method manually Visual Studio autocompletes it with "private void Start()". How to prevent it from doing this? And why does it do that if we have private as a default modifier in C# for methods?

    The same goes with OnTriggerEnter(), OnCollisionEnter(), etc...
     
    Last edited: Apr 20, 2023
  2. For Unity magic methods, it doesn't really matter if they have private accessibility or not. But when you write C#, best practice to always mark private methods explicit private (if you don't write anything, they will be private, that's the default for classes).
    So I recommend instead of removing it when you work with VS, add it when you don't.
     
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,922
    Don’t fight it. It doesn’t affect your work the slightest, Rider does it too btw.

    Even though private is the default it is considered best practice to be explicit. Visualbasic does it the other way round so it can avoid confusion for devs coming from other languages.

    PS: it's "standard" not "stand-art" ;)
     
    Last edited: Apr 20, 2023
  4. Andrew3371

    Andrew3371

    Joined:
    Mar 27, 2023
    Posts:
    23
    The rule in C# is very simple: By default, everything is as close to private as it can be. So I don't want to write extra code and want to make it look clear and straight. With a respect for everyone, mates, I need some help with that thing.
     
  5. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    843
    https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0040
    You can apply dotnet_style_require_accessibility_modifiers = omit_if_default if you really want to.
     
    Andrew3371 likes this.
  6. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,922
    You are not writing extra code. The IDE does it for you. ;)

    Just get used to it. It falls under the category of "don't waste your time on meaningless things". And don't expect applying a single fix to a single install of VS to fix this forever. You may have to re-apply this setting with new VS versions, certainly on different machines, different user accounts, when reinstalling Windows. And if you ever get to share your project with someone else, you'd have to make them apply that change to their system(s) as well and again and again. Don't go there, these subjective "I want more concise code" settings are nothing but timesinks!

    I spent my fair share of time trying to "fight the system" because I didn't like it. Ultimately it's always easier, faster and thus better to just adopt a slightly different code style if everyone and specifically the IDE says that's the way it's supposed to be.
     
  7. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    843
    It's simple to make this automatically persist on any compatible code editor (of which there are many) for the entirety of a project's lifespan with an editorconfig file.
    https://editorconfig.org/
    That said, I'm also in the camp of "don't waste your time on meaningless things." However, while it's not stated in the style guide on docs.microsoft.com, the style guide it's derived from (the one for the runtime itself) says to always specify the visibility. Make of that what you will.
     
    Andrew3371 likes this.
  8. Andrew3371

    Andrew3371

    Joined:
    Mar 27, 2023
    Posts:
    23
    Thank you guys for all this information. I personally tried to figure it out what code style should I use. It's a hard choice for me since Unity itself and many code assets don't follow this rules. Even learn.microsoft site's examples do not use "private", but Visual Studio sometimes adds this private, so I'm confused. Anyways thanks. I wish they made one set of rules for all and followed them everywhere.
     
    Last edited: Apr 24, 2023
  9. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,687
    Tutorials are primarily intended to teach beginners. Microsoft's learning resources don't include them to avoid overwhelming beginners with what is basically a style choice. Some people like to use it and some don't but I recommend becoming comfortable with both as if you work for others you may be required to use theirs.

    You can also disable it through the preferences with these steps:
    1. Open Visual Studio and navigate to Tools > Options.
    2. In the Options dialog box, expand the Text Editor node and then click on C#.
    3. Click on the Advanced node.
    4. In the Code Style section, scroll down to the "Accessibility modifiers" category.
    5. Uncheck the "Insert 'private' modifier" option.
    6. Click on the OK button to save the changes.
     
    Last edited: Apr 21, 2023
  10. Andrew3371

    Andrew3371

    Joined:
    Mar 27, 2023
    Posts:
    23
    Can you specify your VS version? I've searched "Code Style" section for three times and didn't find that option.
     
  11. Andrashhh

    Andrashhh

    Joined:
    Mar 9, 2023
    Posts:
    1