![Image 1](/KB/game/1043237/Windows-Live-Writer-A-Unity_11E71-image_thumb_16.png)
In this tutorial, we will cover creating a simple number guessing game. The user will guess a number between 1 and 10.
You can play the game live at the following link: http://www.adefwebserver.com/unity/numberguesserapp/
![Image 2](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
The computer will tell the user if they are too high or too low.
![Image 3](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
When the user guesses the number correctly, the computer will inform them, and the user can click the spacebar to play again.
Create The Game
![image image](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
The first step is to download and install Unity 5.
Open Unity and create a New Project.
![Image 5](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Call the project NumberGuesser, select 2D and click the Create project button.
![image image](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
When the project opens, click on the Main Camera. then in the Inspector, click on the background color.
When the Color popup opens, drag the dot to the upper left-hand corner of the color pallet to change the color to white.
![Image 7](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Click the Close button to close the Color popup.
![Image 8](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
From the menu bar, select GameObject then UI then Text.
![Image 9](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
A Canvas with a Text object and an EventSystem will appear.
![Image 10](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Hold the Alt and right click-drag to zoom out (or click in the scene and scroll out using your mouse wheel).
Zoom out until you can see the Text Box.
![Image 11](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Click on the Text Box and drag it until it is on the Canvas.
![Image 12](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Ensure that the Rect tool is selected.
![Image 13](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Select the Text Box, and in the Inspector:
- Set the Width to 400 and the Height to 50
- Set the Text to: Guess a number between 1 and 10
- Set the Alignment to centered
![Image 14](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Click the Play button.
The text will display.
![Image 15](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Click the Play button again to stop the program and return to design mode.
Create The Code For The Game
![Image 16](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Select the Text Box, and in its properties (in the Inspector), select Add Component.
Scroll down to the bottom of the list and select the arrow next to New Script.
![Image 17](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Enter TextController for the Name, ensure C Sharp is selected for the language, and click the Create and Add button.
![Image 18](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
The TextController script will be created and display in the Assets folder.
The script will also be attached to the TextBox.
Double-click on the TextController script in the Assets folder to open it.
![Image 19](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Visual Studio will start up…
![Image 20](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
The script editor will open.
Change all the code to the following:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class TextController : MonoBehaviour
{
public Text objText;
int intRandomNumber;
int intGuessedNumber;
void Start()
{
}
void Update ()
{
}
}
![image image](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Basically we are adding two variables that we will later use (intRandomNumber and intGuessedNumber) and a public property that we will use to communicate with the Text element on the game screen (objText).
![Image 22](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Save the page…
![image image](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Switch back to the Unity editor and click on the Text object in the Hierarchy to select it.
Drag the Text from the Hierarchy to the box next to Obj Text property in the Text Controller that is attached to the TextBox.
(This sets the Text Box as the objText property in the script we created. That script will set the text of the Text Box)
Switch back to Visual Studio and add the following method:
private void InitializeGame()
{
intRandomNumber = Random.Range(1, 10);
objText.text = "Guess a number between 1 and 10";
}
Next, update the Start method (that will run one time when the program is started) to the following code (to call the InitializeGame method we just created):
void Start()
{
InitializeGame();
}
![Image 24](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
If you have Visual Studio Tools for Unity installed, you can set a break point (by clicking in the grey area on the left-side of the code file) and then select Attach to Unity…
![Image 25](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Switch back to the Unity editor and click the Play button…
![Image 26](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
…And you will hit your break point.
![Image 27](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
You can hit F5 to continue, and you will be switched back to the Unity game.
![Image 28](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
For now, just select Stop Debugging from the Debug menu in Visual Studio.
Change the Update method to the following code:
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
InitializeGame();
}
}
This will call the InitializeGame method whenever the space bar is pressed.
That method will pick a new random number for the user to guess.
Add the following code to the Update method:
if (Input.anyKeyDown)
{
if (int.TryParse(Input.inputString, out intGuessedNumber))
{
if (intRandomNumber > intGuessedNumber)
{
objText.text =
string.Format("You guessed {0}. You are too low"
, intGuessedNumber);
}
if (intRandomNumber < intGuessedNumber)
{
objText.text =
string.Format("You guessed {0}. You are too high"
, intGuessedNumber);
}
if (intRandomNumber == intGuessedNumber)
{
objText.text =
string.Format("You guessed {0}. \n You are correct! \n (press spacebar to continue)"
, intGuessedNumber);
}
}
}
This will detect what key a user presses and compare that number to the random number created in the InitializeGame method.
If the correct number is guessed, the user is told to press the space bar to restart the game.
Links
Unity 5 Hello World!
Notes
Unity 5 (or higher) is required to run the sample code.