Whenever a class or struct is created, its constructor is called. A class or struct may have multiple constructors that take different arguments.

In other words the constructor looks like a function that will be called when you create instance of class or struct. Also it affects the number of arguments when you create a new instance of class.


Creating constructors

The constructor of class can be created by pressing ‘plus’ button on Class Constructors section:

Note: If you can't find this section, check its visibility in the panel 'Hidden script objects' in the bottom of toolbar.


You can specify constructor arguments in the details window of constructor:


Using constructors

For example you have custom class and you want to set the values of some variables from arguments when class will be instantiated. So create a new constructor and add required arguments and connect (set) them to the variables:

In this example we leave one (first) constructor without arguments to be able to instantiate a class without overriding variables.

After compilation you will able to use this constructor to create an instance of class with arguments that will set the values of variables from arguments:

Note: to select wanted constructor just select it by pressing “<” and “>” buttons.

Render time: 0.01 seconds
136,996 unique visits