Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the default access modifier for a variable in Unity, and why?

This is a somewhat more complicated question than it might appear.

Every Unity video and tutorial that discusses access modifiers that I can find tells me that "private is the default access modifier for member variables in C#", but according to the C# documentation the default access modifier should be internal.

So, the question is, what is the true story of what’s going on here? If I don’t put an access modifier on a variable in a unity script it does act private, but what is Unity doing to make this happen!? Somehow I cannot find an explanation online.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

See also this unanswered question on Unity Answers.

>Solution :

I think you are confused by the scope here: The default access modifier for members (fields and methods) is private, while the default access modifier for classes and interfaces is internal. That’s because classes cannot be declared private (that would mean they’re only visible within themselves, which doesn’t make sense).

It’s good style to always explicitly declare the access modifier for all objects.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading