Visual studio adds "private" to standart unity methods

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…

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.

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” :wink:

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.

1 Like

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.

2 Likes

You are not writing extra code. The IDE does it for you. :wink:

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.

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.

1 Like

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.

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:

  • Open Visual Studio and navigate to Tools > Options.
  • In the Options dialog box, expand the Text Editor node and then click on C#.
  • Click on the Advanced node.
  • In the Code Style section, scroll down to the “Accessibility modifiers” category.
  • Uncheck the “Insert ‘private’ modifier” option.
  • Click on the OK button to save the changes.
4 Likes

Can you specify your VS version? I’ve searched “Code Style” section for three times and didn’t find that option.

Go to ToolsOptions. Select Tools for Unity then change Unity Messages scope from Private to Default. Click Ok and exit.

answer is from here: c# - Unity - Auto completion fix for Visual Studio - Stack Overflow

2 Likes