Unity Interfaces 101

When you are writing the code for your Unity project, it is easy for your to overcomplicate things. You start referencing scripts left and right and create some very unnecessary dependencies where if one script is lost, the whole chain of dependencies …


This content originally appeared on Level Up Coding - Medium and was authored by Mohamed Hijazi

When you are writing the code for your Unity project, it is easy for your to overcomplicate things. You start referencing scripts left and right and create some very unnecessary dependencies where if one script is lost, the whole chain of dependencies breaks down with it.

A very effective solution and to decrease dependencies is to use a new script type called “Interfaces”.

What are Interfaces? And how can you use them?

Definition

An interface is a class type, a container that hold definitions of variables and methods where methods are can hold parameters and must be empty.

Interfaces cannot be added as an instance of a gameObject in the scene nor can it referenced in a script, it can only be inherited by the script.

In other words, an Interface is a contract that holds certain methods that can be called by any script. Let us say you have an Interface I, and 5 scripts. Scripts B,C,D, and E are inheriting interface I, while script A wants to contact the other scripts. Normally you would assign a variable for each script in A and find the gameObjects that hold scripts, B C D & E. But since they inherit from I, then script A can simply call the method in Interface I and do action upon this method without knowing which gameObject holds scripts B C D E.

Real Life Example

In our GameDevHQ mobile project (Mobile Project Progression Report: Enemy Setup Unity), I wanted to have a modular attack system. Instead of referencing every enemy type or any object in the scene that can break, I created an Interface that holds a “Damage” method.

Create a script, make it an Interface, and create a public method that needs an int

In the enemy script, I inherited from the Interface and added the Damage() method and added code to it.

So now, if I want to hit an enemy or any breakable object, I would simply get hold of the interface instead of referencing the enemy scripts.

Quick Tip

As you have scene, you can add parameters to methods in the Interface. But what if you have 3 different scripts and each script want to call a different parameter type for one method. For example an enemy has a float health variable and another has an int health variable.

In this case we can use a generic Interface that will use a generic placeholder, and this placeholder can change from script to script.

Level Up Coding

Thanks for being a part of our community! Subscribe to our YouTube channel or join the Skilled.dev coding interview course.

Coding Interview Questions + Land Your Dev Job | Skilled.dev


Unity Interfaces 101 was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Mohamed Hijazi


Print Share Comment Cite Upload Translate Updates
APA

Mohamed Hijazi | Sciencx (2021-05-30T14:05:20+00:00) Unity Interfaces 101. Retrieved from https://www.scien.cx/2021/05/30/unity-interfaces-101/

MLA
" » Unity Interfaces 101." Mohamed Hijazi | Sciencx - Sunday May 30, 2021, https://www.scien.cx/2021/05/30/unity-interfaces-101/
HARVARD
Mohamed Hijazi | Sciencx Sunday May 30, 2021 » Unity Interfaces 101., viewed ,<https://www.scien.cx/2021/05/30/unity-interfaces-101/>
VANCOUVER
Mohamed Hijazi | Sciencx - » Unity Interfaces 101. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/30/unity-interfaces-101/
CHICAGO
" » Unity Interfaces 101." Mohamed Hijazi | Sciencx - Accessed . https://www.scien.cx/2021/05/30/unity-interfaces-101/
IEEE
" » Unity Interfaces 101." Mohamed Hijazi | Sciencx [Online]. Available: https://www.scien.cx/2021/05/30/unity-interfaces-101/. [Accessed: ]
rf:citation
» Unity Interfaces 101 | Mohamed Hijazi | Sciencx | https://www.scien.cx/2021/05/30/unity-interfaces-101/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.