Using Interface and Implementing it!

Clearing a Tree from the path

I needed a simple way to show UI pop up in my project and to edit the texts fields, so when the object is triggered it will show a different text depending on the object where it is. Sounds cool right!? :)

For example you can use it in situations like if you click a book it says “You found a book!” And when you click a bed “Do you want to rest?”.

So how did I do this… ? With Interface! :) I had never heard of it untill a friend of mine taught me how it works and how to use it. And for real, this was a real lifesaver for me! :D

Enough of me rambling about this, let’s start coding…. :)

  1. So what we need first is the Interface script. There you will write a method and a property that you will call from other classes.
  2. To declare an interface, it’s good that the name starts with capital I and is followed with a capital letter so then it’s IInteractable.
  3. Then you need to create a string property which will be shown in those UI.
  4. And in the end you need a method.
Interface Script

Here you can see the whole Interface script that we need for this. Doesn’t look like much right?

So then we need a Class that implements our Interface script right? Let’s make that happen! :)

Let’s start with Implementing the script!
To implement the IInteractable script we made.
You do it by adding a comma after an inheritance like Monobehaviour.

Implementing Interface in ClearTree script

As you can see above after I wrote IInteractable, I used Visual Studios own option to automatically Implement the script and to add the code we needed. It’s so useful and time-saving! :)

Changing the code
After implementing the interface you got a few lines of code which you have to edit for your own use.
But firstly you need to create a string variable where you will add your text.
After that you can use [SerializeField] to edit the text in the Inspector.
Like shown below.

To get the ineractText you use
public string GetInteractText => interactText;

Setting interactText

What will your Interact() do?
Now when that is done what we need is to decide what the
public void Interact() will do.

Destroying with delay

In my case I needed the Interact to Destroy my tree with Delay after I click it and press the “yes” button.

Text field
After making the interactText, we can edit the text field in Inspectory to say whatever you want! :)

That is all we need for now to get the Interface working.
In other article I will show how I triggered the Interact() after clicking an UI Button in Tree GameObject and using OnClick() event.

Thank you for reading this and I hope you learned something new like I did! :)




From the beautiful snowy country with a touch of "good" humor? Inspired Unity Developer to learn more.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

About Flutter Live 2018

Python Terminal Game Logic and Code

Google Maps Place Results API and the `data` parameter

Dart Code Metrics 4.0: Commands, Monorepo Support, and New Rules

The Six Pillars of AWS Well-Architected Framework

Some Coding Standards for PHP — StudySection Blog

Students Squash 51 Bugs in Inaugural Partner Bug Bash

Weekly Assignment 1

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Janika Suhonen

Janika Suhonen

From the beautiful snowy country with a touch of "good" humor? Inspired Unity Developer to learn more.

More from Medium

Improving Upon Selection Highlighting in Our Game — Part 3

Using Custom Font Family in Xamarin Forms

Variables! — The Building Blocks Of Programming

How to Build a .NET MAUI Barcode and QR Code Reader for Windows and Android