A class is a construct that enables you to create your own custom types by grouping together variables of other types, properties, functions etc. A class is like a blueprint. It defines the data and behavior of a type.


Creating classes

New classes can be created by pressing ‘plus’ button on Script Classes & Structs section:


The class can contain different types of code objects (functions, variables, properties, etc) that can be created in the left toolbar of editor. All unused members can be hidden in bottom menu of this toolbar. In this article described each of them.


Using classes

Classes can be used for holding some groups of information of same type and internal calculation of them. For example we will make a new class that will hold transform and rotate it on defined axis and speed.

Create a class with function “RotateTransform” and variables: TransformRoot (Transform), RotationAxis (Vector3), RotationSpeed(float). Then set “Serializable” attribute to the class to display it in inspector when we will use this class as a variable:


Select all variables, drag them to edit graph and find method “Rotate” from TransformRoot variable. Multiply RotationAxis vector by RotationSpeed multiplied by Time.deltaTime and set them to a second argument of “Rotate” method:


After compiling script we can use our new class as a variable (as array) in other class:

Note: to create one dimensional array of some type just press “+” button near selected type in Variable Type section.

The last step is to iterate through all instances of class in our array and execute function “RotateTransform” in each of them. Connect array variable to the 'Foreach loop' node then find method “RotateTransform” from out pin of Foreach node and connect it to a ‘Loop body’ connector:


Now compile the script and assign it into some gameobject:

As you can see the array of our classes displayed correctly (we set the Serializable attribute) and we can assign some transforms which we want to rotate and angle with speed to each of them personally:



Each class can be inherited from other class and inherit all functionality (variables, functions and other script objects which has ‘public’ modifier) from derived class. To inherit class from other press on the box on the right side of class name field and select the class from the list:

After that you can see that all variables and functions were inherited from derived class:


Overriding methods

To override some method of derived class in inherited class you must to set ‘virtual’ modifier in the function in derived class. After compiling you will able to override it in inherited class:

Note: if after compiling you can’t override the method try to inherit again.

Render time: 0.02 seconds
137,009 unique visits