Adding an item before level start

I’m a beginner trying to learn and I’m just wondering if there is a good tutorial for this or if anyone has any easy methods to do this.

I’m trying to make a 2D game where you can give certain items before the level. The example that comes to my mind is like in Candy Crush games where you pick the level and it pops up asking if you want to start with special candy pieces, you pick the ones you want and then push play and it auto generates those on the board.

If anyone can point me in the right direction I would be very grateful!

You need two things to have this happen:

  • a notion of scene-to-scene persistence like a GameManager, so you can make UI that chooses and remember that answer for later

  • a piece of code that preprocesses the loaded level and adds what you need based on the user’s input.

persistence is easy… statics or singletons will get you there.

Some super-simple Singleton examples to take and modify:

Simple Unity3D Singleton (no predefined data):

Unity3D Singleton with Prefab used for predefined data:

These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

public void DestroyThyself()
{
   Destroy(gameObject);
   Instance = null;    // because destroy doesn't happen until end of frame
}