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
EasySam picks up 2 more coins: make the Console say so.
- Below
int coins = 3;, addcoins += 2;. - Change the
Debug.Logline toDebug.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
1C# Basics in Unity
Hello, UnityVariablesDecimals Need an fText in the ConsoleVariables That RememberIf and ElseCompare NumbersRecap: Coin Collector2Make It Move
Place the CubeMove Every FrameTurn Every SecondSteer with the KeysRecap: Drive the Cube5Vectors
Adding VectorsDirectionsLocal and World SpaceDiagonal SpeedDistance Between ThingsVector3 Is a StructRecap: Drone Pilot8GameObjects and Components
References in the InspectorGet a ComponentMissing ComponentsAdd a ComponentRequireComponentShow and HideRecap: Light Switch6Transforms and Hierarchy
Parents and ChildrenHoist the FlagScaleLook At ThingsMove Toward a PointRecap: Orbiting Moon9Project: Catch the Star
The CatcherA Falling StarPractice on your own: Unity playground