We now have PowerUp for Triple Laer Shot. That’s great, but what if we want to add more diverse types of PowerUps such as increasing Speed or providing Shield? Do we need to create a new C# script every single time we create a new type? No, that would not be the best practice. So, how do we do it? We can create a module that can easily track and add more PowerUps. Let’s get to it!
Creating a variable
Currently, let’s say I want to have a total of 3 types of PowerUp.
- TripleShot / 2. SpeedUp / 3. Shield
In order to create a module, we first need to create a new variable ‘int powerUpID’. This should have the [SerializedField] attribute so that it’s visible from the inspector window.
Then, go to Inspector Window and assign a number to each PowerUp prefab (if you don’t already have prefabs ready, let’s create prefabs first).
We will assign the value as followed:
- TripleShot = 0 / Speed = 1 / Shield = 2
In Player Script
We need to create variables for each PowerUps.
- TripleShot: Use ‘bool’ to activate or deactivate (we created this previously)
- Speed: Use ‘float’ or ‘int’ to increase or decrease number (we created this previously as well)
- Shield: Use ‘bool’ to activate or deactivate.
And, of course, we also need to create StartCoroutine() to start and end these effects.
- TripleShot: we went over this in the previous article.
- Speed: we can set it to 10 when activated and return to 5.5 when deactivated.
- Shield: create a bool statement similar to TripleShot (We will go over how to instantiate this in the next article. Stay tuned!)
In PowerUp Script
Under OnTriggerEnter2D, to distinguish which PowerUp we want to activate, we can use the if statement that will check what PowerUpID number we are looking for, then proceed to call in each method that we created above.
Thank you so much for reading!