Switch Statements to the Rescue!
Let’s make a new “power-up.” But, being the cheeky game designers we are, we’ll make it a negative power-up (power-down?).
We currently have a power-up that maximizes the player’s ammo count (the reddish “star” below). Our new green “star” will decrease the ammo count by five.
We start by adding the ID “DeleteAmmo” to the list of possible power-ups.
That allows us to select the ID from the inspector.
And now we add a new case to the script’s switch statement. In the the case that the power-up’s ID is DeleteAmmo, we call the player’s DeleteAmmo method and break out of the switch statement.
We just need to add the delete ammo logic to the player script, and we have a detrimental collectible!
Now, as I am also an avid player of video games, I would be remiss if I did not point-out that differently colored power-ups are poor design. Especially if they have detrimental effects compared to beneficial power-ups with the same visual. As game designers, we must always be aware of players with disabilities and access to tech. Our color-swap design could adversely affect those with color vision deficiency or monochromatic displays. While I would hope the later is relatively rare these days, it’s something I had to deal with as a kid! While there may always be barriers to entry, we want to be able to share our creations with as many fellow humans as possible!