Let's say I have a variable named button
of type Button
which is of course System.Serializable
.
I want that variable to be assignable through the inspector, so I make it public
:
public class SomeScript : MonoBehaviour{
public Button button;
}
Which gives me that:
Now, I want to keep the code clean & secure (organized) and there's a real reason why I need to preserve the encapsulation.
I know that there are multiple ways of retrieving components on runtime like transform.Find("Some/Genius/TypeUnsafe/Path")
or GameObject.Find("3 seconds scene iteration").GetComponent(...).
I want to avoid that.
How do I mix a variable private access
with it's public access
in the inspector?
private
isn't really intended to prevent a third party from gaining access to a field, it's meant to provide a way to say "don't look behind the curtain at what I'm really doing" - that is, it's mostly a way to keep you from gaining access to it. \$\endgroup\$UI
/API
MVC
/MVVM
pattern for a huge application (a game client actually) and I had to separate theUI
from theAPI
. I needed to assign values through the inspector cause simply - there's no other way.Find()
methods are type unsafe, and it's easier to put things together by dragging & dropping. It's really, really hard to build a classic, OOP framework withUnity
and you have to look for hacky things like this. \$\endgroup\$