Join Alan Thorn for an in-depth discussion in this video Creating a script file, part of Cert Prep: Unity Scripting and Publishing.
- [Instructor] In this chapter we're going to be looking at the fundamentals of scripting inside Unity, and basically scripting allows us to customize the behavior of our game. So, for example, every time you need to play a controllable character, a character that responds to player input, we'll need to use scripting. Every time we need intelligent enemies that can move around the level and contact the player, we'll need scripting. And every time we need interactable objects, for example, if the player character needs to press a button on the wall and then a door needs to open somewhere else in response to connect these events and these actions together, we'll need to use scripting.
Scripting is a really powerful system for customizing the behavior of our games. Now in this course we're going to be looking at the language C#. That is a programming language that was originally developed by Microsoft. It allows us to write instructions that control how our game is going to work; and as we'll see later, these instructions work in a kind of mathematical way. They are a recipe sheet for defining how our game should work. Now Unity supports different types of languages that we can use to define the behavior of our game, but we're going to be focusing specifically on the C# language; and the reason for this is because C# is the predominant language used inside Unity development.
Most of the examples in the documentation, most examples in online tutorials, all of these will use C# scripting. So by understanding how C# works, we're going to be able to create a lot of powerful functionality inside the Unity engine. So in this movie we're going to get started at creating a basic script file and seeing how this fits together inside the scene that we have in front of us here. Now this is a sample, the standard assets Unity project that you can download for free from the Unity asset store. It's also included in the course companion files.
You don't need to use this project to work with C# scripting. C# scripting will work with absolutely any project. I'm simply using this as an example to demonstrate the fundamentals of C# scripting. So here we have a basic scene. This is the AircraftJetAI.Unity scene here. I could press play, for example, on the toolbar to launch playback of the game; and as I do this, we get an aircraft that begins to move around the level. We don't really need to worry about any of that stuff. I'm just going to press play to stop playback.
Now as I start to select objects here inside the hierarchy panel and I select the objects, you'll see from the Object Inspector here that we get a range of different properties that apply to the selected object. You'll notice that the properties are collected together in what Unity calls Components. So, for example, here we have the Rect Transform Component; and I can see the range of that component by simply clicking on the twirly arrow icon to fold up and to expand the component. So by expanding the Rect Transform component, I can see all of these properties in here.
Again, notice we have another component here, the Canvas component, and we also have the Graphic Raycaster component. Now if I start to select different objects in the scene, you'll see that they have different components. So, for example, under the AircraftJetAI we get a range of components here. We get the Transform, Rigidbody, and the airplane controller script. Now by creating new script files inside Unity, what we are effectively doing is we are creating completely new components that can customize the behavior of our object.
So to create a new script file, I'm going to move down here to the Project panel and simply right-click on the background to create a completely new asset. Alternatively, I can choose Assets and then choose Create; or I can move down here to the Project panel and choose Create and then down to C# Script. But I'm simply going to right-click here inside the background and choose Create and then select C# Script from the menu to create a new C# script file. Now the first thing that this will do is it will ask us to provide a name for the script file. And this name is really, really important because a very important rule applies to the naming of script files.
First of all, every single script file inside your Unity project should be named a unique name. You can't have two script files with the same name. This will cause an error, so don't do that. In addition, we want to give the file a meaningful name. That should be a name that tells us something about what the script file is going to do. Now for the purposes of this example in this video here, really this is just a sample demonstration script file. So I'm simply going to call it SampleScript and press Enter on the keyboard. Now in doing this, it will create the new script file, which I can see this in here inside the Project panel; and if I left-click to select the script file, I can see some preview of the contents of that script file here inside the object inspector.
I can even see that the name of the script file that I've gave in here, the SampleScript, this is included inside the preview of the text here. So I can see SampleScript written inside the file. That's why it's really, really important to give your files meaningful names because the name of the file is, in fact, included inside the contents of the script file here. Now, of course, I can simply add the script file to any object I choose inside the level. By default on creating a script file, it does not actually apply to any object in the scene.
It doesn't come to life or take effect. It simply exists here as a static asset inside the Project panel. I have to actually make my script file apply to the object whose behavior I want to customize. So, for example, if I select the jet here, this is the jet object inside the scene; if I wanted to change how this object was going to behave by using a script file, I would typically grab the script file here inside the project panel and drag and drop it onto the AircraftAI object. Now by selecting the object here and scrolling down inside the Object Inspector, you'll see at the bottom here that it has attached the SampleScript here as a component to the bottom of this object.
So right now here is the Object Inspector, and these are all the properties of the jet; and notice that we have SampleScript attached to the object. This script file now lives on this object. Of course, I can attach the same script file to multiple objects. Again, it's attached here and onto here as well; and if I do that, I'm customizing the behavior for all three objects. So all three objects are being now controlled by the SampleScript file. Of course, all the other components still do take effect. The effect is accumulative; so each component, the functionality of each, gets added on to the other.
So I'm just going to select the jet here, and you can see at the bottom we still have our SampleScript attached. Now that's the fundamental process of how we might go ahead, create a script file, add it to an object to customize its behavior. Of course, every newly created script file is, by default, going to do nothing. They're not going to change the behavior of the object. To actually change the behavior, we'll need to edit the script file and insert our own instructions inside C# to customize the behavior of the object. In the next video, we're going to look in more depth at how to set up scripting inside Unity.
- Creating a script file
- Configuring a code editor
- Gameplay mechanics
- Object spawning and deletion
- Getting started with coroutines
- Launching and pausing coroutines
- Cloud Build