Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Unity Basics!
You have completed Unity Basics!
Preview
Itβs time to create our own custom script component and quickly go over what they come with by default.
Unity 6 Documentation
Resources
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Alrighty, things from here on out
are going to start getting more fun,
0:01
but also more complex.
0:04
Just keep in mind that you're not going
to remember everything immediately.
0:06
These things take time
to really make sense, and these videos
0:09
are always here for you to rewatch
should you feel the need.
0:12
I've tried my best to separate everything
into bite-sized chunks so that it's easy
0:15
to come back and find a specific topic
with these videos.
0:19
Now we've come to a point where we need
to create our own custom script component.
0:23
So first, before moving further,
we need to ensure that our IDE
0:27
is connected with Unity.
0:31
So if you're on Windows, go to Edit,
and then Preferences.
0:33
And if you're on a Mac, go to Unity,
and then Settings,
0:37
and then click on External Tools.
0:40
In this select menu for External Script
Editor, select your IDE
0:43
that you're going to use.
0:46
If you don't see it,
0:48
click on Browse and find the application
in your Finder or File Explorer.
0:49
I'm using Visual Studio Code,
so I have that selected.
0:53
Now my scripts will automatically open
in that app.
0:56
With that in place,
let's go down to our project window
1:00
and then into our assets folder.
1:02
I to keep my projects clean and organized,
1:05
so I want to install this habit in
you right away.
1:07
When building bigger games, this assets
folder can get very messy very fast.
1:10
Let's do a little clean up
with what we have already.
1:15
I'm going to hold shift and select
multiple items.
1:18
Let's drag and drop this default volume
asset this universal render pipeline
1:22
settings asset and this input actions
asset into this settings folder.
1:27
Now don't worry about what those mean
or do this early on,
1:32
as it's outside the scope of this course
and we won't be using them.
1:35
While we're cleaning
up, let's go up and right
1:39
click on this Global
Light 2D object and delete it.
1:41
We won't be using any other lights
in this game, and that's outside the scope
1:45
of this course as well.
1:48
Okay, back in our Assets
1:50
folder, let's right-click
and go to Create,
1:52
Folder, and let's name
this folder Scripts.
1:56
Let's double-click it to go inside of it.
2:00
Let's right-click again
2:03
and go to Create, MonoBehaviorScript,
2:05
and hold up right here.
2:09
Before we click anywhere else
or do anything, I need to point out
2:11
the naming of these files
a certain way is important,
2:14
because it's going to automatically name
our class this as well.
2:17
These file names should use
the Pascal case naming convention,
2:21
which means each word in the script
2:24
name should be capitalized
and it should not contain any spaces.
2:26
So let's name this script Player
with a capital P and then press enter.
2:30
It going to start generating our script
as you can
2:34
see it working and compiling in the bottom
right corner here and you'll get this pop
2:37
letting us know this as well.
Now this is going to be a custom component
2:41
and we attach components to game objects.
So let's select our player
2:45
in the hierarchy again.
2:49
Now we could click Add Component
2:51
and search for Player,
and it will appear as an option now.
2:52
Alternatively,
we can click and drag this script
2:55
from our project window and drop it
in this empty space of the inspector.
2:58
And there it is!
3:02
We have our player script
component attached.
3:03
Now the code that we write in
it will directly affect this game object.
3:06
To open the script,
3:11
we could either double-click
the file in the project window,
3:12
or we can double-click
this script property right here.
3:15
This will open it up in our IDE for us.
3:18
We can see when we create a script
in Unity, it gives us
3:22
this starting structure.
3:24
Let's quickly break this down.
3:26
At the top, we have the using directive
for the Unity Engine namespace.
3:28
This gives us access to Unity's core
features.
3:33
We'll use some other namespaces later on.
3:36
Next, we have our class declaration,
which was named Player,
3:38
as I mentioned earlier.
3:42
And quick note,
if your class name and file name
3:43
do not match,
there's probably going to be issues.
3:46
This is why I mentioned it
as soon as we created the file earlier.
3:49
This class automatically
inherits from MonoBehaviour,
3:53
as we can see by the
separation with this colon here.
3:56
This is what makes our script
a Unity script, meaning it can be attached
3:59
to game objects and utilize the Unity
Engine namespace's features.
4:03
All of the code we write for this
4:07
class will be contained
within these two curly braces here.
4:09
Now we have these two Unity lifecycle
4:13
methods given to us and helpful comments
that describe them.
4:15
The Start method runs once and it's right
when the object is
4:19
first created or enabled in our scene.
4:21
This is similar to our main method
in a regular C-sharp class.
4:24
It's perfect for setting things up
and initializing values.
4:29
Then there's Update.
4:33
Update runs completely on every single
frame when our game is playing.
4:34
So for example,
if your machine runs our game at 60 frames
4:39
per second, this method gets called
60 times per second.
4:43
This method is perfect for things that
need to happen continuously, like moving
4:47
an object via the transform component
or checking for the player's input.
4:51
There are other Unity lifecycle methods,
but we don't need to worry about them
4:56
for this course.
4:59
Check the teacher's notes below for a link
to learn more about them.
5:01
I'll also have links to our videos
further explaining
5:04
classes and namespaces
if you need a refresher.
5:06
Alright, that was a lot, and we haven't
even started writing any code yet.
5:10
Let's take a quick breather,
and in the next video,
5:14
we'll start
looking into responding to player input.
5:17
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up