Creating your first game
It's time to make our first game. Unfortunately, I'm here to tell you that it's not an open world massively multiplayer online survival roleplaying game. Instead, we're going to make a simple clicker game. Know that game called Cookie Clicker? Yeah ... We're going to make something similar.
You may be a little disappointed but the good news is, everyone has to start somewhere and the things you learn here will kick off your journey into game development. Let's get started!
Creating a project
The first time you open the Unity Hub, you will be asked to sign in and download the editor. The editor's version doesn't matter too much, so just pick whatever is suggested to you. Once you have the editor downloaded, you can create a new 2D project. You can call it 'MyClicker'.
Setting default code editor
Once the project is created, you will see the default Unity editor UI. It's easy to get overwhelmed, but hang in there ... We will only be working with the following panels for now: the Scene, the Game view, the Inspector, the Hierarchy panel, the Project panel and the Console.
Before we do anything, it will be useful to tell Unity to use VS Code as the go-to code editor.
- go to the top navigation of the editor window
- select "Edit"
- select "Preferences"
- in the preferences window, go to "External Tools"
- in the external code editor, select "VS Code" from the dropdown
To test our changes, we will go to the Project panel, rightclick the Assets folder and create a new C# script. Unity will create a new script file in the C# language, which we will use to make our clicker game. You can name this file "MyClicker". To open it, simply double click it. The file should open in VS Code.
Writing our first game loop
We will begin writing our game. In the file we just opened, you can see 2 blocks of code - Start and Update. These are called methods. They are part a class named after the file it's defined in.
Start
Start is a method, which is called when the game starts. This means that the code we write here will only be executed once. We will begin by creating a variable, which will track our clicks. We will name it "counter". In the start method, we will set its value to 0.
Update
The Update method is called every frame. This can be used to do
something repeatedly. We will use the Update method to check for
player input. If the player clicks the left mouse button, we will
increment the counter by one. To display that the counter updated, we will
use the console. Unity provides a simple way to log to the console via
the Debug.Log() method.
Using our script
We should now have a working script. To test it out, we need to attach it to a Unity game object. We can do this in the Hierarchy panel. Create a new GameObject with a right click. Select it and you will see its properties in the Inspector panel. Drag and drop our script from the Project panel onto the Inspector panel. The script should be added to the object.
Playtesting our game
To play the game, simply click the play button at the top of the editor view. Once the game loads, the play button will be colored blue. Now, if you click with the left mouse button, a message in the console should appear with an incremented counter.
That's it! You made it! If you'd like to continue on your gamedev journey, I have some tips for you.
If not, don't worry. Game dev isn't for everyone. Good job for making it this far! You rock!