In this Unity Scripts tutorial, we explain the main concepts that apply to scripting in Unity. You’ll learn how to use Unity with C# and the tips and hacks to optimizing your scripts.
Scripting is an essential ingredient in all games. Even the simplest game needs scripts, to respond to input from the player and arrange for events in the gameplay to happen when they should.
Beyond that, scripts can be used to create graphical effects, control the physical behavior of objects, or even implement a custom AI system for characters in the game.
Learn more about scripting in Unity: Creating and Using Scripts
Lesson 4 Video Transcript
Hey there everyone. Welcome to another Unity tutorial. In the previous lesson we went over how the Unity Editor works and kind of rearranged it a little bit. We played around with scenes to show how scenes are treated in Unity and we were able to create our own game object.
I’m in the sample scene now where we created our cube. The cube is not very important, so I’m going to delete that and I’m also going to delete the other scene that we had created.
So our project is basically back to how it started before we did the previous lesson. So if you’ve just opened Unity with a new one, if you missed the last lesson for any reason, we’re basically in the exact same spot right now.
So I’m going into the project tab and I’m going to click on assets to get to the main assets folder of Unity, and I’m going to right-click in here and create a new folder called Scripts and we are going to create our first script.
So in the sample scene, I’m just going to create this time, I’ll create a sphere, and let’s just move that to the origin of the scene. We’re going to just put 0, 0, 0 for its position and we’re going to add a component for the first time to one of our game objects. So we have quite a few that come from the sphere.
By default, it needs to render, so it needs its mesh renderer as well as it comes with a collider by default. So if there was collision to happen in the scene, this would have a method to handle it.
We’re going to keep all those for now, they’re not important to what we’re doing, but we’re going to add our own when we add this component. There’s a ton of things you can add. Like for example, we have the mesh renderer that we have up here.
So you can search and find those and add them, but if you want to add your own script, you can add it directly here by typing a new name, like let’s just call it Sphere Script. And then I could press a new script and I could create it and add it right now.
However, there’s another way that you can add these scripts. So I’m going to go into the scripts folder, right-click in here and create a new C sharp script.
This one will be called Sphere Script and I’ll press enter so that it creates it. It’s going to have this wheel spinning down here for a moment, which means that Unity is compiling, it’s making sure that the code is valid. That way if you press play, it won’t have build errors, it won’t have compile errors.
So now that we have the sphere script in here and we have selected the sphere in our hierarchy, we can either search for it.
So as you can see, it now appears right here, or we can drag it from the project right here and now have the spear script. It’s just this tiny little component right here.
We’ve added a script now, but it’s basically empty. It’s the default script. So we could do a little bit more. If we double-click it, it will open our IDE of Choice by default.
Since Unity 2018, it’s Visual Studio and if you watch some of the previous lessons, you would know Visual Studio community is free. And so that’s what I’ve decided to use for these tutorials.
Alright, so it’s going to take just a little bit to open up. It looks like Visual Studio itself is launched. So let me go back into Unity and double-click on the script again. And there we go. Now we have the script itself opened.
I’ve zoomed in by holding control and zooming in on the mouse wheel. You don’t have to do that of course, and I’ll just break down the script really quickly. You have the name of the script and it inherits the mono behavior class.
What that means is it is a script that you can drag onto the object. As we saw in Unity, we can drag our script onto the object that becomes a component.
If you don’t have the mono behavior keyword here, if you don’t inherit from mono behavior in some way, you can’t drag the script onto your object. This comes by default. It’s no problem. All of this is every time you do a create a new C sharp script, it’ll generate this for you.
All we want to do is make our sphere do something interesting. So there are a couple of things we can do.
The first thing I’m going to do is use the start function that was provided when we generated the script, and I’m going to get our spheres scale to just increase a little bit just immediately when we start. It’s just instantly going to be a different size.
So if you remember, the scale is held in the transform component. So I can just type transform and then I need our scale. So if I type scale, you can see there’s no scale variable, but we have local scale that’s good enough. This variable is exactly what you see in the inspector in Unity.
So if we hover over local scale, you can see it’s a vector three. A vector three is three kind of like coordinate points put together. So we want to set this equal to a new vector three, and let’s make it a little bit uneven. Let’s do five for the X value. Let’s do three for the Y value and 10 for the Z value.
And then of course at the end of every line, we’ll save the script. You can watch the asterisk could disappear when I save and we’re going to alt-tab back into.
So in Unity, it takes a second to compile. Since our project’s new, it’s really fast, it’s already done and you can see the script itself hasn’t changed at all. But when we press play, the sphere will scale to this weird size.
So you can see its scale is 5, 3, 10, just like in the script. And because this was done in play mode, when we exit play mode, it will revert back to the size here.
If in the scene we change this to be a hundred for its X scale and pressed play, it would scale to 5 3, 10, and then when we exit play mode, it would revert back to a hundred that we put in. Obviously, that’s a bit ridiculous, but that’s generally the flow of how this works.
Now let’s say we wanted something with a little bit more motion on it. We would not want to use the start function.
Instead, we can use update. As you can see, update is called once per frame. That means it happens approximately 60 times a second. This is another function that Unity and C just support by default.
The mono behavior class lets us do a lot of things by default without needing to kind of, we don’t need to invent a frame rate. Unity already has that for us. So by this function, we can kind of use it.
So let’s take this line of code. I’m going to highlight it and I’m going to cut it. I’m going to paste it. So we’re going to just ignore start for now. We can even delete it.
Nothing wrong will happen if we delete it and we’re going to get rid of this part here. So we still want transform local scale and all we’re going to do is we’re going to multiply it.
So that’s the asterisk, and if you want to multiply it by a number immediately you do times equals. So we’re going to just make it get a little bit bigger.
Every frame, even this is probably a little extreme, let’s go real small. So it’s just going to get a tiny bit bigger. Every frame, 1.01 F, basically it means it’s a float value and a float is just a number with a decimal.
Basically, that’s all you need to know about floats. So if we do this every frame, it’s going to try to scale the sphere up a little bit while we’re in play mode. So as you can see, it’s going up and it’s scaling like crazy.
And there you go. That’s how you use the update loop. Again, start only happens once right when you press play or when a scene is loaded, but update will happen every frame. So this will just continuously happen so you can kind of start to picture how you might want to program movement or something like that.
The last thing that I want to show for this video, since this is our first script, it’s super basic stuff. Hopefully, it’s been pretty easy to follow along so far, is just make it so this won’t happen unless we press something on the keyboard. So first we’re going to make this go a little bit bigger.
We’re going to make it multiply by two and I’m going to write it the other way this time. So I’m going to say transform local scale is equal to transform local scale times two. You don’t need the 0.0 F, you could just use two, but I prefer to just write out all my numbers as floats.
Now this will happen every frame right now, but we can check to see if a condition is met and this condition will be if we are pressing the space bar.
So if we have the input get key down key code space, then we will do this. So if you see how this code’s written here, we have a condition here which is the if statement, and it needs to evaluate input, which is something unity can kind of look like for us.
That’s another thing that Unity provides is the input class. We can check if the key is down, which is once every time it’s pressed, and we’re looking for key code space.
So the space bar, you can see there’s a lot of different things here. You could just do five, which is alpha five. You can do the equal sign, which is equals, or of course, just sticking to space.
If this happens, then we will multiply our scale by two.
So let’s switch back into Unity, and I’m going to press play once it’s finished compiling and I’m going to press and hold space.
As you can see, it’s gone up once. I’ve now let go of space and every time I press it now it will multiply its scale by two. If you wanted it to go up whenever you were holding space, you could get rid of this part where it says down and this would check that it’s held at all.
So the first frame gets pressed as well as every frame after that that it’s being held. This will be true and it will call this part of the code. So if we leave it like this, you’ll see it’s going to get very large, very, very quickly. So let’s give that a shot.
I’m going to press it pretty fast, but as you can see, even though I press it pretty quick, it’s still got quite a few frames of multiplying its scale by two, and if I hold it, it just explodes in size.
But yeah, that’s basically everything that I wanted to show for this video. Again, it’s still pretty basic stuff, but it’s the really important stuff and this is the kind of thing that you’re going to see in every tutorial.
Basically from now on, creating scripts, adding components to your game objects, and then actually writing some code into your scripts is extremely important stuff. And this is the most beginner-level version of it right now.
But in the coming lessons, we’ll be looking to do some more interesting things with input and movement, maybe having a character move around a little bit, or at the very least, having an object move around in the scene and I’ll show you how to switch scenes as well.
So look forward to that. That should be coming pretty soon.