Using Custom Classes within Databases in Unity3D

Pete Thomasberger
3 min readJul 30, 2021

--

Objective: Create a custom Class for defining information about 3 customers to use within a customer database.

Step 1: In Unity, create an Empty GameObject in the Hierarchy window called Customer_Database. Then create a C# Script called CustomerDatabase and drag it onto the Customer_Database GameObject. Lastly, create another C# Script called Customer and open both scripts.

Step 2: In the Customer script, remove the Start() and Update() methods, as well as MonoBhaviour. Then, above the Class designation, use [System.Serializable] to make the Class information visible and editable within Unity’s Inspector window.

Step 3: Create the variables for each type of data that is needed for the customer.

Step 4: Create a public Constructor called Customer with parameters that correspond to the customer information variables. The variables will be assigned to the parameters, and this will basically create a reusable template that will generate the information fields for the customer data.

Step 5: In the CustomerDatabase script, create three public variables of type Customer called customer1, customer2, and customer3.

Step 6: Within the Start() method, assign the Customer Constructor to each customer variable, adding the required data within the Constructor’s parenthesis.

Alternatively, an array can be used to generate the Customer data fields within the Inspector. Create an array of type Customer called customers above the Start() method.

Save the script, go back into Unity, and the Customers array (green) will be available to input the customer data, but also, the three empty Customer variables (red) above the array populate the hard-coded data once the scene is in play mode.

--

--

No responses yet