A list system using only IF and ELSE
Once you get started on creating more and more power ups, you realized that it is easier to just re-use the same “power up” script since it follows similar if not the same behavior.
How to Utilize the IF statement in a list
The logic behind this behavior is simple:
“If the player isn’t using triple shot as a power up, then it shall be something else, therefore we go down the list until we found the correct one.”
The power ups would basically go down a hierarchical list and trigger the desire one.
To start, we must identify each power up game object with an integer. This would allow the if command to trigger the correct instructions. Using a private integer value and naming it power up ID will give you an text input in the hierarchy.
In the hierarchy, we can manually assign each power up the desired ID. Integers start from the number zero. So in this example, zero would be triple shot, one would be speed increase, and 2 would be shields.
Using the if statements is quiet straightforward, start with if and initialize the private integer “power up ID” and give it a number starting from zero. As we recall, we wanted zero to become the triple shot, so we call upon that method within the if statement. Then in the “else if” statement we continue down the list of power ups.
Now the semi modular system is functional, but this is only possible because we only have 3 different types of power up presuming that we won’t have anymore. As you can figure out, using all those if statements is A) not very optimized and B) not very effective. And as with all scripting within Unity there is a better method. This would be covered in the next article.