C#CodingGame DevScript-writingTrainingUnity

Explore Unity – Getting Started with Scripts

In this project, you are introduced to the concept of using IDEs (Integrated Development Environments) like Visual Studio, which is my personal preference. I have used Visual Studio a lot over the years and really enjoy many of the add-ons that assist with debugging and writing good C# code, which Unity leans on heavily. As such, Visual Studio is typically installed along with Unity and you can check to see if you already have it by looking at Unity > Preferences and selecting External Tools, and looking at the dropdown menu for External Script Editor. If you don’t have it already, you can easily install it via the Unity Hub.

example of a basic Unity script

Scripts are just another type of GameObject, only they run code that causes things to happen in your game depending on what you write. For this project, the first script you write teaches you the basics of a script and writes some text to the Console Log in Unity. By placing a line in the Start() method of the script, you can send a message to the Console Log once when your game starts. If you move the line to the Update() method of the script your code will run with each frame of your game, which is good when you want to constantly check for status changes or updates.

By adding variables to scripts you can customize your code from within the Unity Inspector. Modifying GameObject properties from within Unity is a great way to change aspects of your game and test different variables without having to load Visual Studio and rewrite your code every time you want to make a change. To make a variable accessible from the Inspector you need to declare it as public, tell what type of variable it will be, and assign it a name. Variables in a script are always declared before the Start() method. Follow up by adding lines of code in the Update() method to transform your GameObject. Examples are the transform.scale component, which can be used to change the size of the GameObject, the transform.position component, which can be used to change the location of the GameObject, or the transform.Rotate component, which can be used in the Rotate() method to change the rotation of the GameObject.

This project was just a brief introduction to scripting in Unity and there is so much more that can be done. I have actually written entire games in Visual Studio using nothing but C# programming. By integrating it within Unity you can achieve some very powerful results and take your games to new heights. For now, though, this is all there is on this step of the Unity Essentials Pathway and it’s time to move on to the next project.

Leave a Reply

Your email address will not be published. Required fields are marked *