Menu
CoddyTech

Text in the Console

Part of the Unity Scripting Basics section of Coddy's Unity journey. Lesson 4 of 85.

A string is text: a name, a message, a score line. You write it between double quotes, and its type is string:

string hero = "Mia";

The + sign glues strings together, and it turns numbers into text on the way:

int gems = 4;
Debug.Log(hero + " found " + gems + " gems");

That writes Mia found 4 gems in the Console. The spaces live inside the quotes: hero + "found" would give Miafound.

A variable can change. gems = gems + 2; reads the old value, adds 2 and puts the result back, and gems += 2; is the short way to write the same thing. -=, *= and /= work the same way.

challenge icon

Challenge

Easy

Sam picks up 2 more coins: make the Console say so.

  1. Below int coins = 3;, add coins += 2;.
  2. Change the Debug.Log line to Debug.Log(playerName + " has " + coins + " coins");, with the spaces inside the quotes.

Press Play: the Console shows Sam has 5 coins. Then Check.

Try it yourself

using UnityEngine;

public class Greeting : MonoBehaviour
{
    void Start()
    {
        string playerName = "Sam";
        int coins = 3;

        Debug.Log("Hello");
    }
}

All lessons in Unity Scripting Basics

Practice on your own: Unity playground