Creating Collectables in Unity

Jordan Evans
Nerd For Tech
Published in
3 min readAug 12, 2021

--

Now that our player movement has been fleshed out a bit, let’s make the spheres within our game become collectables for our player. To start, we will want to create a couple of scripts, along with a canvas element for us to work with. For the coins, what we can do is apply the script to 1 of the objects, and then apply the change to all of them:

From here, we can just rename all of our collectable’s to coin, and create a UIManager script and attach it to a canvas:

From here, we will figure out where we want to have our coin tracker placed, along with the size and colour to work with:

Now to get started scripting our coin collection method. To start, we knw that we will want our coin to have a trigger method attached to it, along with having our player know if it has collected the coin, and the UI element to be updated every time we collect 1:

Player Script Logic

For our player script, we are simply going to create a public void that our coin script can interact with to add to our total, which can then be fed towards our UI script to add that total to. We will also need to make sure we create a connection with our UIManager through getcomponent so that we can interact with that script:

Next, within our coin script, we need to create that logic for when we collect the object, we add 1 to our coin score as well as destroy the object so that the player can’t just sit on the 1 coin and artificially inflate their coin total:

Finally, we can go to our UIManager script and create a link towards the text that we will want to alter so that whenever a coin is collected, we can have it updated on our screen. Once we have all of this in place, we can check it out in our game:

Now that we have a functioning coin collection system, we can look towards create some moving platforms to help our player reach new places.

--

--