Using Switch Statements to Create Modular Powerups

David Hunter Thornton
3 min readSep 10, 2022

--

Objective: Change my PowerupLogic Script to be more modular and function with any new powerups I make.

The first step to this to actually bring in the new powerup and attach the PowerupLogic Script to it.

The problem at the moment is that this will give the new Speed Boost PowerUp the same functionality as the TripleShot. So let’s create a Serialized Field and define each powerup by a numerical value.

Now I need to set each PowerUp to the relative number.

Next is actually defining the Switch Statement logic to filter the PowerUps with only specific triggers. Its important to make sure to include the “break;” at the end to separate each section.

Also for the moment I’m just using “Debug.Log” until I actually implement the features for each Power up.

If for some reason we collect a Powerup that doesn’t have a numerical value assigned to it, I’ll create a “default” trigger in the Switch statement to notify me instead of throwing errors.

I also want to setup the spawning logic for the SpeedBoost. I started by creating a SerializeField for the current maximum number of powerups in case I add more in the future.

Next I want to create a method for generating a random number between 0 and 1 less than the maximum number of powerups.

Now the spawn logic can be updated with another switch statement.

After collecting powerups for a bit I checked the Console log and everything is working correctly!

You can now play this game on itch.io! I’ll try to keep it updated as I work on it and add new features or tweak existing ones.

……………………………………………………………………………………….

Shameless Plug: If you like my stuff, be sure to check me out on other social media platforms and join the BornAngry Games Discord!

YouTubeTwitchLinkedInFacebookTwitterPortfolioDiscord

--

--