Polymorphism in Unity

Those who are trained in computer programming should be famaliar with the term, but “Polymorphism” is an object oriented term for hiding variable access.


Unity is a bit looser.  If you want to set something in the inspector- such as a reference to a prefab or a component’s parameters- you can usually set it as a public variable and it’ll work for you.  But…you don’t always want to make it public, because when you access that component’s functions, you’ll end up with something like this in Visual Studio:



All you really have to do is mark it with tags, such as:

[SerializeField] private GameObject prefabDead, prefabAlive, prefabYourMom, prefabYourFemaleCanine;


On the other hand, you may want to make the object public, but not accessible by the Inspector:

[HideInInspector] public GameObject explosionInstance;


That’s all for now.  Feel free to ask questions.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s