- Home /
protected static in Unity CS?
It seems that any method called by a public static function has to be public as well..
If it's a method that should really only be accessed from the same public static class, would using the "protected" keyword work?
Private functions can be included in public functions.
public static class Scopes
{
public static void Public()
{
Debug.Log("Public!");
Scopes.Private();
}
private static void Private()
{
Debug.Log("Private!");
}
}
Answer by paulbuck86 · Mar 14, 2019 at 03:14 PM
I'm surprised this hasn't been answered. Yes, protected works to limit code access within the class or derived classes. However, I personally like to use "protected internal" since it acts like an OR statement and allows my code to be accessed namespace wide.
Probably worth explicitly pointing out the huge error in the question: methods called by a public static function do not need to be public (any more than they need to be static). This aspect of the question is a complete red herring.
Also "internal" does not restrict "namespace wide" but assembly wide across all namespaces. When and how to use access modifiers should not be personal taste but a software design decision. Like TreyH said in the comment, private does work just fine within the same class. protected is only used when subclasses of the class should also have access to it.