Serialized Custom Classes in Unity

Richard Morgan
2 min readJan 29, 2023

Let’s say we need an array of things for our game. Things could be items, or characters, etc.. All the things share certain traits, so we’re going to create a custom class for our things (superheroes), use a monobehavior script to initialize the objects, and serialize the custom class so it appears in the Unity inspector like this:

We’ll need two scripts, the SuperHero class and the SuperHeroDb class which we will add to a game object.

For the SuperHero class, we’ll:
1) define traits declarations and make public for the inspector
2) create a constructor function and assign it to an instance of the class (an instance of a class is called an object)
3) serialize the class to make it readable in the inspector

For the SuperHeroDb that will initialize new objects from the SuperHero class, we’ll:
1) declare an instance
2) initialize the instance (in Start) using the constructor we created in the custom class
3) build a method to create objects
4) add a public array for the inspector

Next we add SuperHeroDb to a game object and when the game runs, the superheroes are populated and displayed in the inspector along with our array and any items the user might add using the inspector.