Hello World and My First C# Game In Unity

Home / Technology / Game Development / Hello World and My First C# Game In Unity
Week 1 of my Summer of Code was a success! It included a great refresher of C#, Unity, and Visual Studio before moving into my first text-based C# game. Follow this and the rest of my game development journey at blissfullemon.com/hello-world-and-first-cs-game.

Week 1 of my Summer of Code was a smashing success! I spent around 8 hours total coding and working on this post, and I truly believe every second was well-spent. I had a great refresher of C#, Unity, and Visual Studio, all of which I had a bit of experience with in the past. Then I completed my first C# game in Unity!

I’m also proud to announce that with this post, I have completed all of my goals for this week. It’s important to celebrate every tiny victory, but even more important to keep going after the fact. So, let’s just jump into this because I still have a ton of work left to do!

Task 1. Download and Install Unity 2018

I downloaded Unity 2018.4.3, the latest release of Unity 2018, which Unity 2019 replaced earlier this year. I opted for the older version because it correlates with the version used in the Complete C# Unity Developer 2D course. Check out the full 2019.1.0 release notes on the Unity website to learn about the changes made in 2019. 

If you want to use the newest version and follow the Udemy course, that is OK! The course instructors actually recommend using the latest stable version of Unity. I am ignoring that advice, though. I can’t tell you how many times I’ve tried to follow a course using a later release than what the instructors had, only to run into issues with deprecated code or other unsupported features. Besides, Unity has a really cool hub that will let me download multiple versions on my computer and switch between them seamlessly. So that’s what I plan to do!

Task 2. Review Unity Documentation

This was one of the easier tasks this week because I discovered that I already had the documentation bookmarked. I also discovered a little gem on the Unity website. 

While navigating to the download page, I discovered that there are more learning tutorials and guides on the website than I remember, including an entire section called Unity Learn. It contains free and premium access to tutorials, projects, and courses. Now I’m even more excited about the fact that I gave myself the last week to finish up on anything that I wasn’t able to do! If I stay on track and keep to the schedule I’ve set for myself, I will be able to use the last week to go through some of these other tutorials – maybe even some of the more advanced ones.

Task 3. Section 1: Introduction & Setup

The first section of the Complete C# course was easy to complete, especially since Unity was already installed. It’s worth mentioning that I used Unity quite a bit a few years ago and even developed a few small games during that time. So, some of what I am learning is really just a refresher of what I already knew, but other things are brand new. The UI looks mostly the same, but there are a few features that have really changed. I also admittedly remember very little of the C# that I learned back then, but it is coming back to me quickly.

With this first course section, I created my first HelloWorld.cs script, which is truly a rite of passage in learning any new programming language.

HelloWorld.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HelloWorld : MonoBehaviour {    
     // Start is called before the first frame update    
     void Start()    {        
          print("Hello World!");    }    

     // Update is called once per frame    
     void Update()    {            
}}

Scripts in Unity have to be attached to a game object, so in this case it was attached to the camera.  Once you run the game inside Unity, the script is immediately executed and prints “Hello World!” in the console.

Console output for Hello World

Finally, I took a little quiz testing my knowledge of the concepts covered in this section and scored a 5/5 on my first attempt.

Task 4. Section 2: Number Wizard – Basic C# Coding (My First Game in Unity)

The second section of the Complete C# course was the perfect opportunity to really dive into C# and creating my very first game. It wasn’t technically my first game, but it actually did feel like it since it has been so long since the last one.

Over the span of just a few hours, I was able to completely refresh my memory on the basics of C# while creating a basic game console log game. The object of the game is to ask the user to think of a number between specified minimum and maximum values. Then the game guesses what number the player picked and asks if the number is higher or lower. Based on that answer, the game makes another guess, repeating the same higher/lower question until it eventually guesses the correct answer.

While Number Wizard admittedly won’t win any Game of the Year awards, it was really nice to take such this basic approach so I could focus more on the code and less on game dynamics. And the best part is I re-learned everything I need to know to create a basic game or program in C#.

I’m not going to get into every tiny little detail. However, some of the concepts from this week included:

  • variable types
  • initializing and declaring variables
  • concatenating strings and variables
  • responding to player input based on the keyboard listener for up/down/enter
  • using conditional statements
  • local/global variable scope
  • performing mathematical operations
  • detecting and fixing bugs
  • creating and calling functions/methods
  • refactoring code
  • encapsulation

The Number Wizard C# Game Code & Output

The entire Number Wizard game is scripted in one NumberWizard.cs file. There was a bit of refactoring done to the original code, but in the end it all looked a bit like this:

NumberWizard.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NumberWizard : MonoBehaviour
{
    int max, min, guess;

    // Start is called before the first frame update
    void Start()
    {
        StartGame();
    }

    void StartGame()
    {
        max = 1000;
        min = 1;
        guess = 500;

        Debug.Log("Welcome to wizarding world of numbers --- or something nerdy like that.");
        Debug.Log("Pick a number, any number... Well, almost any number.");
        Debug.Log("You can't pick a number higher than " + max + ".");
        Debug.Log("And you can't pick a number lower than " + min + ".");
        Debug.Log("Got it? OK! I'll make the first guess. Is your number is higher or lower than " + guess + "?");
        Debug.Log("UP arrow = higher, DOWN arrow = lower, ENTER = correct answer");

        max++;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            min = guess;
            NextGuess();
        }

        else if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            max = guess;
            NextGuess();
        }

        else if (Input.GetKeyDown(KeyCode.Return))
        {
            Debug.Log("Ermahgersh! I got it right! I knew it was " + guess + " all along. Let's do this again.");
            StartGame();
        }''
    }

    void NextGuess()
    {
        guess = (max + min) / 2;
        Debug.Log("OK. Is it " + guess + "? Am I right, or is your number higher or lower?");
    }
}

Assuming your first guess is 40, the console output would look like this, which includes an automatic loop to start back over at the beginning:

Number Wizard console game, scripted in C# using Unity and Visual Studio
Number Wizard console output for user guess of 40

First Impressions of C# Game Developer Course

My overall first impression of this course is really positive. As a somewhat experienced programmer (at least in the academic sense), it is certainly one of the easiest courses that I have ever taken. I’m sure it will get more difficult as time goes on, but the the instructors, Ben and Rick, do such a great job explaining the concepts and helping you to absorb the material through practice and repetition.

If you look at the code from the Number Wizard section above, you’ll notice that there were several Debug.Log() statements, despite the fact that this could have all been done at one time. This not only makes the code easier to read for a beginner, but it also forces you to type and retype those statements over and over again.

More importantly, though, the instructors do an amazing job with teaching you the most important concept in programming – you have the tools and abilities to learn anything you want to learn. It’s not about knowing everything all at once and becoming a master programmer overnight. It’s more about knowing that there is information out there, you just have to Google it. There’s nothing wrong with not knowing everything as long as you know how to problem solve and search for the answers.


So, what do you think? Don’t forget you can enroll in the Complete C# Unity Developer 2D course over on Udemy. You can also follow along with my journey by entering your email address in the box below to get an alert when I make a new post. ❤

Join 1,467 other subscribers
Week 1 of my Summer of Code was a success! It included a great refresher of C#, Unity, and Visual Studio before moving into my first text-based C# game. Follow this and the rest of my game development journey at blissfullemon.com/hello-world-and-first-cs-game.

Let's chat!

%d bloggers like this: