Reduce, reuse, recycle — A quick guide to writing modular scripts in Unity

Luke Duckett
Apr 12, 2021

--

Does it make sense to make a script per powerup/item/enemy considering their functionality is the same for the most part?

It may be ok when making a couple of powerups but what happens if you want to change their base functionality? This is why Unity developers must write scripts to be as modular as possible.

Let's take our powerup example. We currently have a triple shot ability and now we want to add a speed boost.

We can add an identifier that can be assigned in the inspector to differentiate our powerups.

Now we can edit our powerupID on our speed powerup so they are both unique.

Next, we can add some logic to determine which powerup the object is and use this to activate the players' abilities.

And our final result

That’s all for now.

--

--

Luke Duckett

🎮 First Nations Unity Dev from Wonnarua country 🏞️ | From Player to Lifelong Learner: Crafting Games, Debugging Code, and Embracing New Technology