Pick Up Everything!- Game Dev Series 80
Objective: create script to collect objects in game.
Collectables in platformer game mean a lot. Mostly, they are the biggest achievement that players pursuit.
In this article, we will create the function of collectable that allow Player to collect and update the counts to UI.
Update the scene
Before we coding to add these function, we need to update the scene.
First, I changed the name of collectables to coin, just like what we get in Super Mario Bro.
Second, create a Canvas with text object. This is where we are going to display the current coin counts.
With these done, we can start collecting these coins.
What should Player do
First thing first, we will define the meaning of coins to Player. In Player script, create an integer variable to store coin counts. And also create a public method to calculate the total counts of coin that we collected.
What’s more in this method is to update the coin to UI, which will be done when we finished created the script of UI.
UI controller
Create a script called UIManager, and also create a public method to update coin text.
I have used Singleton to create UIManager. This would make it easier to access the public method in it.
Once we have the method to update UI, we can add one more line in Player.
And that is all for Player and UI. Time to collect these coins.
Those collectables
The function of collecting objects would be similar to the function we created to collect powerups in Galaxy Shooter.
Create a script and called it Coin. With this script, we will use OnTriggerEnter to detect if is Player collided with.
Once it collide with Player, make it communicate with the public method we created. And don’t forget to do the null check to prevent compiler error.
After communicated, this gameobject should be destroyed.
All done! Back to editor and assign the text gameobject to Canvas, and everything would works now!