- Home /
What's the point of declaring a C# script a public class?
Public. Why bother? I never used it, and never had a problem. But Unity 3b4 introduced this issue where if you do not use it, and then you declare a public variable, you'll get a warning if you don't give the variable a default value. But I don't want to do that, because I use public variables purely so I can assign them in the Inspector! I reported this warning issue as a bug to UT, and support told me about how to solve it by using "public" in front of the class name. That's a lot cleaner than having to use "= null" or whatever, after all the public variables, but it still seems useless to me.
It seems you've already solved the problem of getting a warning on public variables so I'm not sure what your exact question is. You might consider moving this to the forum where you'll likely get more constructive discussion.
$$anonymous$$y exact question is "Whats the point of declaring a C# script a public class?".
Answer by jashan · Aug 10, 2010 at 04:27 PM
Actually, I'm a bit surprised that Unity does allow you to get through without the "public". When omit the "public" it means that your class is accessible "internal" which means
The type or member can be accessed by any code in the same assembly, but not from another assembly.
(Source: Access Modifiers (C# Programming Guide))
In Unity, as your scripts are accessed from the game engine runtime which is not your code assemblies (all scripts of the same language get compiled into a DLL; editor scripts and I think also plugins are compiled into separate assemblies), actually scripts which are not declared public shouldn't work, IMHO, because they are accessed from outside their assembly.
So, in general, I'd always create public classes unless I really want my classes to only be within the current assembly (which is rarely the case - would be the case for very special purpose helper-classes, though).
For a deeper understanding of encapsulation (which is what these access modifiers are all about), Wikipedia is a good starting point: Encapsulation (object-oriented programming). Another nice section about encapsulation can be found in the main Object-oriented programming article.
When you omit public it's private, not internal. Doesn't make much difference to unity though since it's done via reflection
@$$anonymous$$ike - "By default, a class without any access modifier is defined as internal ". From Essential C# 2.0 by $$anonymous$$ark $$anonymous$$ichaelis. Which also concurs with the $$anonymous$$SDN link that @Jashan gave.
Fun! I must have been thinking of something else then. Back to holidays! :p
@Jessy: An "assembly" is a file with the extension *.dll which contains the compiled bytecode of the classes/script that you define. It's a bit tricky because people sometimes call them "DLLs" which is correct, of course - but could be misleading (a DLL is originally a "dynamic link library" which is a binary file used on Windows and specific to Windows). When working with Unity, you usually don't have to directly deal with those "assemblies" unless you include 3rd party assemblies to add APIs you're using (like log4net, for instance).
Answer by Cyclops · Aug 10, 2010 at 01:39 PM
I can't say what UT's reasons are, but if you simply want to get rid of the warnings, you can (in C#) use pragmas - Disable warning messages, for instance
#pragma warning disable 0168 // variable declared but not used.
Read it from the Unity warning line when it happens. I think there's a prefix of some characters, ignore them.
Your answer

Follow this Question
Related Questions
When exactly do scripts have to be named exactly the same as the class they contain? 3 Answers
Color set to black even though it's set by the editor 0 Answers
Accessing not yet publicly added scirpt 3 Answers
how to activate a public void from another script 1 Answer
CoD 4 "weapon classes" 2 Answers