Creating modular systems with Switch

Jordan Evans
Nerd For Tech
Published in
5 min readMay 17, 2021

Now that we have spent some time learning about using switch, let’s use it in relation to our game. As we plan to have multiple types of powerups, it makes no sense for us to create a new script for every one as we have a perfectly working one already. However, we have to let our game know that when we collect a different sprite, we want that sprite to affect our player difference. For starters, we are going to place our speed boost sprite into the hierarchy and add the components that are needed:

Once we add the rigid body, remember to set gravity to 0. With the box collider, we shall put the trigger option on as well as shrink the box so that it properly shows the size of the sprites collection radius. As well, we shall apply the powerup tag to it, and place it in the foreground layer so that it doesn’t disappear behind the stars. Next, we will set up our animation quickly. Lastly, we will apply our powerup script, drag it down into our power prefab folder and delete our object.
Now that we have the sprite set up in our editor, we can switch over to our script and get our new lines of code setup:

Now that we have our switch statement in place, we can check the logic in our game to see if when we collect our speed sprite, it shows “Collected speed boost”.

Now that we can collect our speed boost, let’s get our code setup so that we can actually have a use out of it. First, we have to go over to our player script and implement some code to apply a speed boost amplifier once we collect the sprite.

We simply have to create a speed boost active bool, just like our triple shot, and then a multiplier bonus for our speed. From here, we can create a line of code that is applied when we have our speed boost active.

All we simply need to do is create a if statement for when our speed boost is true and multiply our translate by the speedMulitplier so that we go quicker. Now that we have that set up, we have to apply code so that we can turn our multiplier true.

The method we use for this is the same as that of our triple shot powerup. Once all of this is done, we have to go over to our powerup script and change the code in the case1 line to activate our new powerup.

After this, we can check our results in the game and see what happens.

Now that we have our speed boost working proper, we need to get it spawning. As we have multiple powerups now, we will want to have it randomly choose 1 powerup to spawn in the window we have set so that we don’t have a predictive pattern to follow.

With this method however, there is an issue that pops up. As we can see in the inspector for the player, the speed value doesn’t show 10 as what we desire. So, we have to alter our code slightly so that in public void we created below, we adjust the speed value there, and thus we don’t need to have this extra if line above.

First, we have to set it to multiply in the void portion, and then with our coroutine, we will want to divide the values back down to regular so that we don’t constantly multiply our speed every time that we gain a speed boost.

Now it’s time that we get our spawn manager to work with multiple powerups. With Unity, we can implement something called an array, which allows us to create a list of objects we can attach to.

To set up an array, all we do is add [] to the line of code that we wish to create an array for. From here, we can go to our editor and add how long we want our list to be along with dragging our prefabs to each respective line.

Now we can go back to our script and adjust the spawn code slightly so that it will spawn 1 of our 3 current powerups at random.

What we have to do to adjust our script is create a new variable to work with. As for the range, we enter in the low and high end of our array. We will then place it next to our instantiate portion of the script and test it out in our game.

As we can see, our powerups spawn randomly over our random period of time just as intended. Now that our speed boost is going and our spawn manager pops out random powerups, it’s time we give our third powerup it’s code so it has a use.

--

--