Learning Unity — Making Our Objects Modular And Reusable

Joshua Nielsen
3 min readJun 22, 2021

--

In the last article, we learned how to make our in-game effects temporary. This time around, we’re going to learn a system for making our game objects more modular and reusable.

Let’s consider an example from an existing video game — Super Mario Brothers. The Super Mushroom and the 1-Up Mushroom in that game have certain similarities — they have a similar look, they move the same way, they get acquired the same way. But they have very different effects when they are collected.

If you’ve been following my articles on learning Unity since I started, it would be understandable if you thought that every game object that had unique logic driving it would need its own individual script. However, there is actually a very simple method for having objects with similar functionality all use the same script. In other words, if we were coding Super Mario Brothers, we would not need completely separate scripts for every mushroom in the game.

The trick is to use a serializable field to identify the individual object. With that information, you can then use conditional logic in your script to make them behave differently. Let me provide a simple example of changing the object’s color depending on the value of a serialized field.

Within Unity, we will need to create two game objects and a script. Apply that one script to both game objects, then open the script in your editor.

Script level variables

These are the script level variables I have created for this example. The color id variable is the value that will identify what we want from the script. Thinking back to our example with Mario, we would assign each mushroom powerup with its own unique numerical identifier and keep track of it.

The next variable is just a sample list of colors in an array. It’s what we will be drawing from to assign a new color to our objects.

Since we will be fiddling with the color of the object, the final variable is intended for our object’s Renderer. Be sure to assign that variable in the Start method.

With the variables in place, the remainder of the code is simply taking our serialized value and changing the color based on it.

Changing the color based on our serialized field

Now within Unity, we can set the color id value in the Inspector window for any object using this script. For example, if you set one value to 1 and another value to 3, one of your objects would be blue and the other black.

Color values set through the serialized value

Of course, this is a very simple example. More realistically, in your own projects you would have much more complex logic determining what happens.

Returning to our Super Mario example once again, our different mushrooms would each have a unique identifier applied them. When Mario “collides” with the mushroom, the identifier would be used to guide the script to either growing Mario or giving him another life.

One problem with this approach, however, is that you could have a lot of different options to sift through in your code. Imagine if you had 10, 20, or even 50 different powerups to consider. That’s a lot of if else statements to consider.

Fortunately, there is a solution, and it is the subject of my next article — the C# switch statement. Until then, good luck and happy coding!

--

--

No responses yet