Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Now that you've learned how to write and implement an interface, let's get some more practice implementing interfaces.
C# Documentation
- Interface Keyword - This section of the MSDN C# Programming Guide details the interface keyword.
- Interfaces - This section of the MSDN C# Programming Guide details the concept of interfaces as it relates to the C# language.
Resources
- Object-Oriented Programming in Unity - Project Files - This is a zip file that contains the Unity project for this course. Download the file, unzip it, and then in your file browser (Finder on OS X and Explorer on Windows) navigate to Assets > _Scenes. Finally, choose the scene you'd like to open.
-
0:00
[MUSIC]
-
0:04
Let's play the game for a moment and try shooting arrows at the scoreboard.
-
0:11
So I'll hit the Play button and start the game.
-
0:16
And let's shoot a few arrows at the scoreboard.
-
0:19
[MUSIC]
-
0:24
And when you're done, stop the game.
-
0:27
The arrows just bounce off the scoreboard.
-
0:30
And don't stick like you might expect.
-
0:33
There's also no sound and
-
0:35
nothing interesting happens when the scoreboard is hit.
-
0:38
Let's use the interface, iTargetable,
-
0:40
that we've created to make the scoreboard react to being hit by arrows in some way.
-
0:47
In the hierarchy, select the scoreboard game object.
-
0:51
Then in the inspector, add a new script called scoreboard hits.
-
1:02
This will be the script that implements the interface iTargetable so
-
1:06
that we can make the scoreboard react in some way.
-
1:11
Move the script from the Assets folder into the Scripts folder.
-
1:16
And then open the scoreboard hit's script for editing.
-
1:22
Before we begin, let's take a moment to think about
-
1:25
what this script is going to do and then how we'll do it.
-
1:30
When the arrows hit the scoreboard, we want them to stick to it and
-
1:35
then the scoreboard should react in some way.
-
1:38
Let's say that for the first two arrow hits
-
1:41
the scoreboard just plays a wood impact sound just like the targets, but
-
1:45
every third time the scoreboard gets hit we'll spawn the wooden plank
-
1:50
particle system and play the wood debris sound to indicate that perhaps
-
1:55
the scoreboard is being damaged in some way by us firing so many arrows at it.
-
2:00
So, how should we do all of that?
-
2:02
Well, let's go back to the script.
-
2:05
First, we know that we're going to need two audio clips.
-
2:09
One for the impact sounds and one for
-
2:12
the destruction sound when there is wooden planks flying everywhere.
-
2:16
So let's make those variables first.
-
2:19
At the top of this class we'll create a serialized field.
-
2:25
And this will be a private audio clip called impact sound,
-
2:33
and then same thing another serialized field which will be a private
-
2:38
audio clip called destruction sound.
-
2:44
Then we'll need a place to put the game object that spawns the wooden planks.
-
2:49
So let's create another variable for that.
-
2:51
This will be a serialized field and it's a private game object,
-
2:59
and we'll call this destruction particles.
-
3:05
Then every third time an arrow hits the scoreboard
-
3:10
we want to spawn those destruction particles and play the destruction sound.
-
3:14
So how will we know how many times the scoreboard has been hit?
-
3:19
For that we'll need to create an integer that will count upwards by one
-
3:24
every time the scoreboard is hit.
-
3:26
This doesn't need to be exposed to the inspector because
-
3:29
it's just keeping track internally.
-
3:32
So we'll type private and
-
3:35
arrowHits and by default that will be set to zero.
-
3:42
Great.
-
3:42
There's one more piece of functionality here.
-
3:45
How can we get the arrows to stick to the scoreboard?
-
3:50
Well let's go back to the arrow collision class for a moment.
-
3:55
It says here that, when an arrow enters into a collision,
-
4:00
if the other game object that it's colliding with has the interface
-
4:05
iTargetable, then the arrow should stop moving.
-
4:09
And it should be parented to the target game objects so that it's stuck.
-
4:15
So that means, all we have to do to make the arrows stick to the scoreboard,
-
4:19
is simply implement the iTargetable interface on the scoreboard Hit's class.
-
4:26
Because it's attached as a script component to the scoreboard game object,
-
4:30
which already has a collider attached as well for the arrow to hit.
-
4:36
So let's go back to the scoreboardhits class and
-
4:41
implement the interface I targetable.
-
4:43
Remember when we implement an interface we type a comma
-
4:49
after the derived class name followed by the name of the interface.
-
4:55
To finish this interface implementation,
-
4:58
we need to type the method target reaction.
-
5:03
In fact, let's save this script now and return to unity to see what happens.
-
5:10
If we open the console window, it says scoreboard hit's does not
-
5:15
implement the interface member iTargetable.Targetreaction.
-
5:20
So what that means is that we didn't finish implementing the interface and
-
5:25
we broke the contract on functionality.
-
5:27
We need to fulfill that contract by writing the target reaction method.
-
5:32
So let's jump back to our script and do that now.
-
5:35
Down towards the bottom.
-
5:40
We'll type public void TargetReaction.
-
5:48
For now, let's simply write this method with nothing inside the curly braces.
-
5:52
Remember, this method needs to be public because
-
5:56
the AeroCollision class needs to have access to the method so
-
6:00
that it can call it when an arrow hits a targetable script.
-
6:05
We're going to remove the start and update methods for
-
6:08
now, because we won't need them.
-
6:12
And then save the script, return to unity, save the scene and then, play the game.
-
6:25
Now when an arrow shoots the scoreboard It will stick to the scoreboard.
-
6:36
They don't do much else other than automatically destroy themselves after
-
6:40
a certain period of time, but simply by implementing that interface we were
-
6:44
able to get some basic functionality without doing much else.
-
6:48
[MUSIC]
-
6:50
Now stop the game.
-
6:54
We did part of what we wanted to accomplish.
-
6:57
But what about all of those other variables for sounds and particles?
-
7:01
That's what we'll work on next.
You need to sign up for Treehouse in order to download course files.
Sign up