Test the code by pressing Play in Unity and then clicking on the Play UI button. The SetGameState
function that we just added to GameManager
should hide the MenuCanvas
. Oops! Something is wrong. Unity is displaying an error in the Console window. Something surely went wrong. Let's take a look at the red error message, which is shown here:
In your programming career, you will come across many issues with the games or applications that you are creating. I have deliberately asked you to follow my steps to cause this issue. We will learn with experience. Beginners in programming often rely on luck while sorting issues. They blindly change something, test again, and keep going in that loop until they fix the issue by pure luck or simply give up on trying. This is a very bad approach to debugging. I want you to understand what the issue is. In most cases, Unity will try to give you an accurate description of the error in the console. Once we face an issue, we will learn to understand what's wrong and only then will we be able to fix the code issues. While learning something new, we will gain experience and write flawless code.
Let's take a look at the Console window again. As you can see, the console is divided into two sections:
Our error says: UnassignedReferenceException: The variable menuCanvas of GameManager has not been assigned. You probably need to assign the menuCanvas variable of the GameManager script in the inspector.
Hmm... To see the exact line that is throwing up the error, double-click on the error in the console. MonoDevelop should open after a few seconds, selecting the line that is causing the issue:
We are not asking Unity to do much in this line. What we are trying to do is set the Canvas component stored under the menuCanvas
variable name to false
. What can go wrong then? An unassigned reference exception means Unity is trying to use a variable that is not properly assigned. I led you to this issue deliberately, as this is a very common mistake made by beginner programmers.
Still confused? Not sure what I mean? Select the GameManager
game object and take a look at your GameManager
component in the inspector, as follows:
It should be clear to you now. We forgot to connect the Menu Canvas slot with the MenuCanvas
game object. To fix this, simply drag and drop the MenuCanvas
game object into the Inspector just as you did before.
Make sure that the Clear on Play toggle in your Console is switched on. Press Play. The error should disappear and everything should work as expected.
Great! We have created a menu view with a simple, fully working button. The Menu view will be the first view seen by our user. It would be good to include a game name in this view.
Download and import GameLogo.unitypackage
from the Packt hub. The GameLogo
prefab will be imported into the Assets/Prefabs/UI
folder. Simply drag and drop this prefab onto the MenuCanvas
game object.
A very simple and ugly game logo will appear on the Menu Canvas!
Feel free to change the name of the game and anything else you wish. Hey! At the end of the day, this is your game, right!