GameDev : UI Manager!
Why we need the UI Manager?
Same reason why we need a Spawn Manager : we need an object to be responsible for UI, and only for that.
We create an empty object, name it UI Manager, create the script with same name and attach it.
TIP: Another method to create the script is this.
Go to the object, add component, new script and rename it.
This way the created script will already be on the object.
Now open the script, and think to this : what is the SCORE SYSTEM?
What type of logic we need to implement?
Like everything we’ve done so far, we’ve set up each system after thinking about : “How is this game mechanic supposed to work?”
Score System works like this : after destroy an enemy, it give me “n” points to add to my total score.
To implement this, we need another variable in our Player : score.
Open Player, and [Serialize] score variable
Now, Player has score.
What’s we need?
We need to check if enemy dies, it give us some points to add to our score.
In this case, we need to go inside Enemy script, and in the Collision with Laser, we need to add points to our score.
We fire the Laser, enemy dies and it give us points.
This is the order, because THE CODE IS EXECUTE IN THE SAME ORDER AS WE WRITE IT.
Destroy the Laser, give us points, destroy itself.
This line means two things: we need a method inside Player to give us the score and we need another variable in Enemy to add THOSE POINTS TO OUR SCORE.
In the Score method we add score, but we have to COMMUNICATE WITH UI MANAGER TO CHANGE THE UI WITH THE ACTUAL SCORE.
In UI Manager script, we need to TALK with Text component, to make it change every time we destroy an Enemy.
And the way to do this is the same when we have to communicate between scripts, we need a reference to that component.
Now, remember when we talk about namespaces?
To access the UI Component, we need the “LIBRARY” OR “NAMESPACE” ABOUT THE UI.
Inside UI Manager script, we need a reference to the Text.
If we just type Text, we have an error.
Because we don’t have the reference of the RIGHT NAMESPACE, and it is
UnityEngine.UI
With this Namespace we can access to every component of UI System, and we can create the reference to Text
Now we can it in the Inspector, and can ASSIGN IT
To make the things more easy there is one thing we can do, and it is perfectly fine.
We have a Canvas and we have an object, the UI Manager. It can be a little redundant, so it is absolutely ok DELETE the UI Manager and attach the UI Manager script DIRECTLY TO THE CANVAS.
It make sense for you?
The important thing is to have a script DEDICATED ONLY TO UI.
To finish, we assign in Start the reference to the Text component
For best practice, every time we take a component make a check if that Component is Null.
Then we assign Score : 50, just for try it.
Press Play…
..and we have a strange error…
Why we have a NULL REFERENCE EXCEPTION?
First look at Canvas, maybe we miss the Text component in UI Manager
But we have here the Text component…
So what is the problem?
This is one of the “types of problems” we will run into a bunch of times at first.
Recap what happened to us :
we have the Text, we grab the component to cash it in Start and make a check for the NULL REFERENCE.
Than, we initialize to “Score : 50”.
But we have an error.
Let’s look at this :
We have a Canvas, we have a UI Manager script into it, and we assign the Text component in UI Manager.
The problem is :
“We try to grab a component in UI MANAGER SCRIPT, but the COMPONENT WE NEED IS NOT ON CANVAS, IS A CHILD OF IT. SO we need to grab a component, but IN CHILD.”
We used GetComponent<> in the previous article, but in this case we need GETCOMPONENTINCHILDREN<>
Test it
Now it works.
See you in the next chapter to learn more about UI System!