Main

Learn INTERFACES in 5 minutes | Unity

Learn how to use interfaces in this quick Unity tutorial. In this example, we will use the IInteractable interface to interact with any object that implements that interface. ✅ Get my courses with discount: 👉 Unity Input System with Rebinding: https://www.udemy.com/course/unity-input-system-rebind/?couponCode=NEW-INPUT 👉 Unity 2D Master: https://www.udemy.com/course/unity2dmaster/?couponCode=NEW-START-MASTER 👉 Unity Mobile Course: https://www.udemy.com/course/unitymobilecourse/?couponCode=NEW-START-MOBILE 👍 Save your time and learn Unity faster ------------------------------------------------------- 🔔 Subscribe for more awesome content: 🔴 https://www.youtube.com/channel/UC_N2G2u2ySuWH3DgkXkpCMw?sub_confirmation=1 ------------------------------------------------------- 🟡 Support this channel through donations Any amount means a lot and will help grow the channel: 👉 PayPal: https://www.paypal.com/paypalme/rootgames ------------------------------------------------------- Social Networks: ⭐ Instagram: https://www.instagram.com/rootgamesofficial/ ⭐ Twitter: https://twitter.com/RootGamesCRO ⭐ TikTok: https://www.tiktok.com/@rootgamesofficial?lang=en ⭐ Facebook: https://www.facebook.com/Root-Games-103370388608233 ------------------------------------------------------- #unity #interface #interfaces #unitytutorial

Root Games

8 months ago

Hello everyone! In this tutorial, I will show you  how to use interfaces to interact with different objects. For example, I can open and close  the door, I can open the chest, or I can start interaction between characters. Every object has a  different implementation using the same interface. If you don't use interfaces, your code would  look something like this. You would try to get a different component for each object  and call a function from that script. This can work fine if you have a cou
ple of objects to  interact with. But if you have a lot of objects, this is not practical. We can replace all  of that with this. Let's see how it works. To create an Interface, we will create a  new C# script. Since we want to interact with other objects, call it "IInteractable."  Naming is optional but it's common practice to put the letter "I" in front of the desired  name. Like "IInteractable, IDamagable or ISubscribe" and so on." In the script, we  can delete default functions, and we don't
want to inherit from MonoBehaviour. Then let's  replace "class" with the keyword "interface." Now we define function or functions.  I'm going to define a function called "InteractLogic." Notice that the  function is empty, with no functionality. Interfaces are like contracts, every class that  inherits, or better say implements this interface, must implement this function. What the function  will do is defined by every class separately. Let me show you. Here I have the "Chest" object.  I will e
xplain other components in a moment, now let's open the "Chest" script. To implement  the interface, write a comma after Monobehaviour, and then the name of the interface. You will notice one error. Since we inherit from the interface, we must implement the function.  To finish implementation we can right-click, go to "Quick actions and refactoring" and choose  the option to implement the interface. That will create a public function "InteractLogic." Nice.  Now we can determine what this functio
n will do. As you can see, here I have one "OpenChest"  function that will change the sprite of the chest and disable the collider. You can do whatever  you need. Then we call it inside "InteractLogic." Now let's implement the interface to other  classes. I will open the script on the "Door" object. Again, the same procedure. Here I want  to use the function that will open and close the door. Notice that every class has its own  implementation of the same interface. And that is the idea. The las
t implementation will  be for this little guy. For example purposes, I just enable the object that starts the  animation. I repeat, inside the "InteractLogic," you can do whatever you need. Now we need to  somehow call the "InteractLogic" function. Let's go to Unity. I use the new input system.  When I press "E", I want to try to interact with objects. If you want to know more about the  new input system, please check my courses. Next, I will select the player and  open the "PlayerControls" scri
pt. This is the function that is called every  time I press "E." Inside this function, we need to call "InteractLogic." So let's  create one "IInteractable" instance. First, we will check that the instance is  not null, then we call "InteractLogic." Of course, somehow we need to populate  this instance and assign value. Create one public function "SetIInstance "  that will take one "IInteractable" parameter. Then set our instance to the interactable  object that we pass through the function. We
also need one public function  that will set the instance to null. In Unity, make sure that the  player has the tag "Player." If we for example select the Chest, you  will see that it has Rigidbody2D and BoxColliders2D. The collider needs to be  a trigger. You can adjust it as you wish. In the "Chest" script we need a  reference to the "PlayerControls." We use OnTriggerEnter2D and OnTriggerExit2D.  When any object enters the trigger, we check the tag. If that is the player, then  get the "Player
Controls" component and pass this object as an instance. The idea is that we  can pass any class that implements the interface. When the player exits the  trigger, then clear the instance. You can do the same for the  door, and our friend "Crabby." Let's test it. I will enter the trigger. The  instance is set. I can press "E" and interact with the object. The same is for the door or any object  that implements the "IIneractable" interface. Hope you learned something  new and have a nice day.

Comments

@Soundy777

Straight to the point, concise & informative! Brilliant explanation!!

@PoisonedAl

Once you learn Interfaces and Events, life gets a lot simpler and your code WAY cleaner. Get scriptable objects in there too and you can remove a lot of your dependencies.

@ThanhNguyen-gj1pm

I went through your tutorials and it was so helpful that I decided to buy your course to support you.

@motero2k

I really like your videos. This video is short but very useful. Awesome!

@gamesbyspinola

really cool !! Very useful ! Thanks

@enliik

Good! But it’s better to place OnTrigger event inside player

@richardrothkugel8131

ISubscribe. You got me.

@Fyres11

It is indeed a way to do it, but what about going the other way around? You could put the OnTriggerEnter and Exit only once onto the player. When its in range of a trigger, set a bool _canInteract to true and _interactable = other.GetComponent<IInteractable>(); (Set back to false and null in exit).

@NoobProMaster01

Can you make a tutorial for ground enemy chase trigger?

@zeusdelolimpo2882

quee bienUCkszU2WH9gy1mb0dV-11UJg/hf90Xv-jHeOR8gSxxrToBA

@user-yu1hg8ql1t

why doesn't my vs code report an error when not implementing the interface? should I type it down for each class?🥲