Classes

Chris Didier
5 min readApr 2, 2021

I will now be working with classes. Now I have been working with monobehavior classes to this point. Mono classes allow the user to attach scripts to game objects. Monos are great for behaviors like a laser that is going to shoot up or a player that is going to move around. In classes it is important to break down code into modular methods to improve readability and troubleshooting. Classes allow us to create clean modular programs that organize well.

It is always good to create a custom class based around the object you are working on. With a custom class you can have something called an constructor. It is named the exact same thing as the actual class. You can also add parameters into constructors.

I now will be showing an item database for an RPG like game. Serializing classes in Unity allows the user to be able to see a class’s components even if it is not on the monobehavior script that is on the game object, but through references the scripts communicate.

My next challenge was to create a program with customers in a database. I will need multiple traits for the customer they are First name, last name, age, gender. and occupation.

Next, I’m going to create a RPG Spell System. To do this I first had to ask what was required when having the ability to cast a spell. Those requirements were the name, level required, which item was required, and how much experienced would be gained from using the spell. This information went into a non-mono class. Next I created an array of spells, asked what is my current player level. If the space key was pressed the program would look through the array of spells and after determining the players level would say which spell was used and how much experience was earned from the attack.

Next, I will be working with class inheritance. Inheritance enables new objects to take on properties from existing objects. So continuing the RPG them we have items and each item will have a name, itemID, and icon, but you will have more items than that. You will have weapons with their own unique attack strength, food for restoring health, and money.

Next I’m going to be working with the bank system using inheritance of classes. Things we know are every bank has a name, location, and vault full of money (at least I would hope it has money), we can also deposit, check our balance, and withdraw. The other type of bank we will be adding is a federal credit union and it will determine if you are approved for a lone and for how much.

Some information we don’t want to be accessed and changed. So we want to protect some data. This allows only classes that are child classes to have access to those that inherits the parent scripts.

It is important to know we can inherit classes that are monobehaviors. Say you had animals and you wanted to hear their sound. You could create a pet class and have a method of speak. Well now you have a specific animal and you want that specific animals sound. Create that animals class and on the method in the animal class change it to a virtual method. This allows for an override in the specific animal class.

This was a really valuable section, and I was able to learn a lot more about classes and how they interact with each and gathered a better understanding of when to use mono vs non-mono classes.

--

--