In this video, Jesse Freeman goes over the basics of how to create, modify, and attach scripts in Unity. Jesse focuses on C# scripting in Unity 5 and also how to edit those scripts with external editors such as ModoDevelop, which is included in Unity and Visual Studio on Windows.
- [Instructor] In order for us to get started, we're going to need to create a new project. Once you have Unity open, select the new project button. Let's go ahead and name our project CodingInUnity. And to make things a little bit easier, we're going to go ahead and switch to 2D mode. While you can still code in either 3D or 2D mode, 2D mode presents you with a much simpler looking editor. Next, let's turn off Unity Analytics and once you've picked a location where you want to save your project, go ahead and select create project.
Once you create a new project, you'll be presented with a default view in Unity. One of the interesting things about Unity is that the IDE itself actually runs an instance of Unity and you can configure the editor by writing C#, just like you can with building a game. While we won't cover that in this course, it's important to know that a lot of the same techniques you learn for building games will also enable you to build tools and extend the functionality of the editor later on.
Scripts in Unity are attached to game objects. Any instance in a scene is considered a game object. Let's take a look at the default game object that is added to every scene when you create a new project. If you look in the hierarchy panel on the left hand side, you'll see we have a camera attached to our scene. By selecting the camera, you'll pull up a small preview window of the camera and by going into the inspector, you can take a look at all of the settings of the camera.
Each of these are components attached to the camera and a component represents a C# script. This game object, called main camera, has a transform component, a camera component, a GUI layer, a Flare layer, and audio listener component. As we build our scripts, we'll attach them to game objects and they'll show up in the inspector as components as well. One of the interesting things about Unity is that you can actually modify the scripts through the inspector.
As you can see here on the transform component, there are properties for the positions x, y, and z value. If we change these in the inspector, when the game runs, the changes from the inspector will override any of the default settings on the transform component itself. This pairing between the code and the visual editor is what makes Unity very appealing. Once you start setting up scripts and components on game objects, it's easy to modify them and customize them inside of your scene, without having to go back into the code editor.
As you can see, towards the bottom of our screen, we have the console already open. The console allows us to display messages from our code. It'll show warnings that the compiler finds in our code and it'll also show us errors. We'll be using that a little bit later. Now let's talk about how to actually open the coding side of Unity. To do this, let's go to the Asset menu, and at the bottom, select open C# project.
This is going to open Unity's default editor, MonoDevelop. As you can see, there's no code inside of our project, so MonoDevelop is going to open to a blank screen. While MonoDevelop is the default editor of Unity, there are actually some alternative editors you can use, depending on what platform you're developing on. While you may have heard of Visual Studio before, Visual Studio Code is a much more lightweight, cross-platform version of a text editor.
Recent versions of it have had built-in support for Unity and you can use it as an alternative to MonoDevelop on Mac or PC. If you're coding on Windows, you may want to check out the actual Visual Studio IDE. This is a much more robust IDE that focuses primarily on editing in C# as well as a few other languages. Microsoft also has built-in support for Unity in Visual Studio and it's the IDE I use primarily when I'm developing on Windows.
One other thing to note, especially for Visual Studio, is that you can actually add plugins to enhance your C# developing environment. One of the best plugins out there is made by a company called JetBrains and the plugin is named ReSharper. By installing ReSharper into Visual Studio, you will get additional tools that'll aid you in developing your code, much more advanced refactoring, code correction, and better inspection telling you where errors are as you work, are all part of ReSharper.
Unfortunately, ReSharper isn't free and it's a paid plugin, but as you get more advanced into C#, it's well worth the money. Inside of Unity, you can modify the default code editor by going to the Unity menu on a Mac and selecting Preferences. Inside the Unity Preference window is a tab for the external tools. Here you can see it's using Internal, which if we look at the dropdown, has MonoDevelop as the built-in tool. All you need to do is select browse in order to choose the other IDE you want to use.
Let's go ahead and type in Hello World. With our script selected, you'll see the code inside of the script inside of our inspector window. But we're not able to actually edit code inside of Unity itself. In order to open this up in MonoDevelop, simply double click on the scripts icon and you'll be presented with a code inside of the editor. This is a default Unity script. It includes code that Unity is going to need at the top of the file and all the code is wrapped inside of a class.
You're also presented with two default methods, Start and Update, which Unity will automatically call. Start will be triggered at the beginning of the class and Update is called during the game as Unity goes through and runs the game loop. We'll talk about this in a little bit more detail later on. The last thing I want to do is switch back over to Unity and show you how to attach a script to a game object. Here, you can simply select the script from our folder and drag it onto the Main Camera.
If we select the Main Camera, you'll now see that our Hello World script is attached to it at the bottom of the inspector. Before we move on, let's go ahead and save our scene so that we can continue to work in it throughout our course. Simply hit Command + S on a Mac or Control + S on Windows, and you'll be presented with a Save window. Let's go ahead and save this inside of our assets folder and we'll just call this HelloWorldScene.
- Scripting in Unity
- C# variables and functions
- Lists and arrays
- Conditions and loops
- C# classes