How to use the Unity console

Learn the basics of programing with Unity! Starting with how to use the colsole.

Be sure you have Unity installed and either Visual Studio, or Visual Studio Code on your machine.

With that out of the way, let’s go over the basics of programming and how to use the Unity console.

To start off, let’s make a new Unity project named “Unity Console”.

Take some time to familiarize yourself with the different parts of the Unity display. Today we’re going to focus on the project/console section.
In the Project panel let’s make a new script in the Assets folder. Right click > create > C# script and name it HelloWorld. Double clicking the file will open it in your editor of choice.

The script will be pre-populated with two methods, Start() and Update(). The Start method runs once when the script is first created, and the Update method runs once every frame. We’re going to ignore Update for now and focus on Start.

Type the following code into the Start method.


string myString;
myString = "Hello World!";
Debug.Log(myString);

The first line is a string variable declaration. Learn more about variables here!
Line 2 assigns the myString variable with the value Hello World!. And line 3 prints the value of myString to the console.
The file should now look like this.


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


public class HelloWorld : MonoBehaviour
{
   // Start is called before the first frame update
   void Start()
   {
       string myString;
       myString = "Hello World!";
       Debug.Log(myString);
   }


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

   }
}

Let’s test It out!
Go back into the Unity editor and click on the Main Camera located in the Hierarchy panel. That will change the Inspector panel to show all of the components on the selected object. Select the Add Component button at the bottom of the Inspector panel. In the search box that opens, type “hello world” the name of your C# script

Now that the script is being used by the Main Camera object, the Start method is going to run. Press the play button at the middle top of the Unity editor and switch to the Console panel. If everything worked correctly, you’ll see the following output.

Congratulations! You just ran your first Unity project!

Task

Now that you know how to use the Unity console, try to complete this lesson’s task.
Modify the program by adding three new variables.

  • A string variable `myName`. Give this the value of your name.
  • An int variable `myAge`. Give this the value of your age.
  • And a float variable `ageInCenturies`. Give this the value of your age divided by 100.
Use string concatenation and Debug.Log() to print the following message to the Unity console.
Hello, my name is myName. I am myAge years old, and ageInCenturies centuries old!

FAQ

Why is my age divided by 100 always 0?
This is probably due to using only integers in your math expression. If you have an expression like `27/100`, the computer will truncate the result since the value is going to be an integer. But if the expression is changed to include a float `27/100f`, the result is going to be a float and will include the decimal places. Learn more about Truncation vs Rounding.

I’m getting the error, “Cannot implicitly convert type 'double' to 'float'.”
This can be caused by trying to assign a float variable a double value. The incorrect code could look like this...

float ageInCenturies = myAge/100.0;
100.0 is a decimal value. To correct it, put an `f` at the end of the number to indicate that it’s a float value...
float ageInCenturies = myAge/100f;

Recommended posts

We have similar articles. Keep reading!

Ship Movement Tutorial

Video tutorial about ship and camera movement in a rail shooter built with Unity3d.

Caching and Apache

Quick tutorial covering how HTTP headers affect browser caching and configuring Apache for your needs.

Enemy Health and Explosions

Video tutorial about managing enemy health in a rail shooter built with Unity3d. We'll also discuss killing enemies in fiery explosions.

A* Pathfinding Tutorial

Technical talk from Matt Bauer about A* pathfinding in Nauticus Act III.

Comments

Log in or sign up to leave a comment.