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.
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.
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.
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.