Adding a Modular Power up System
Next objective is to introduce a simple Modular Power up System. First we create a Script that we can attach to different prefabs, each representing a different Item relative to its ID. We pass the information to the Player.cs that will be responsible for executing the logic affecting the Player. We can then spawn the Item when an Asteroid is destroyed.
Item Class
Say we have different power ups called ‘Items’ (you can name it as you prefer). Each of the Items will be responsible for adding a power up to the Player, be it a negative or positive one.
Let’s add a few power ups that can be referenced with an ID, the logic here is very simple. We assign the item ID in the inspector or elsewhere through code.
Then we need a reference to the Player.cs because it’ll hold the actual logic for various power ups affecting the Player.
Now we create an OnTriggerEnter() method in which every Item ID has its own code block, representing what each power up is supposed to pass through to the Player.
If the other GameObject we collide with is tagged “Player” and is not null, we pass relevant information to the Player activating the respective logic for each power up.