O'Reilly logo

Unity 2D Game Development Cookbook by Claudio Scolastici

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Adding collectibles to the game level

Running on platforms is not enough for the prototype; we need to provide a player with a goal. Let's say that our prototype level is complete once the player has gathered a number of collectibles that we randomly scatter in the level.

To achieve that, we add a few lines to the PlatManager script and create a new prefab to be instantiated as our collectible game object.

Getting ready

Open the PlatManager script in Monodevelop and be ready to add the lines described here.

How to do it...

  1. We need two extra variables to make the collectibles: a public Transform to store the reference to the collectible prefab and a private one to instantiate it. Do this by adding the following lines to the script:
    public Transform collectPref; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required