Constructors and variable initialization question

I was reading Writing_Scripts_in_Csharp_26_Boo.html in the documentation, and it made me wonder…

If I understand correctly, constructor should never be used in any class which inherits from MonoBehavior, which means they can be used in classes which don’t and which will never be attached to a gameObject…

so just to make sure, is the code below okay or should I not use a constructor?

using UnityEngine;

public class BikeUpgrades {

    // Singleton access
    private static readonly BikeUpgrades _instance = new BikeUpgrades();


    #region Private variables

    // Camera switch variables
    private float _camSwitchDuration;       // Duration in seconds of the time the 2d camera will last
    private int _camSwitchCount;                // Amounts of camera switch available to the player

    #endregion

    #region Constructor
    public BikeUpgrades () 
    {
        if( _instance != null )
        {
            //Debug.LogWarning ("Cannot have two instances of singleton.");
            return;
        }

        _camSwitchDuration = 2.0f;
        _camSwitchCount = 1;      // Does not call the "set{}" accessor
        CamSwitchCount = 1;       // Does call the "set{}" accessor
    }
    #endregion

    #region Getters and Setters
    public static BikeUpgrades Instance
    {
        get{ return _instance; }
    }

    public float CamSwitchDuration 
    {
        get{ return _camSwitchDuration; }
        set{ _camSwitchDuration = Mathf.Clamp( value, 2f, 10f ); }
    }

    public int CamSwitchCount  
    {
        get{ return _camSwitchCount; }
        set{ _camSwitchCount = Mathf.Clamp( value, 2, 10 ); }
    }


    #endregion
}

Thanks in advance for your time,
Stephane

1 Like

The Constructor is fine to use, even in MonoBehaviours if you understand how Unity creates the Inspector.

The Inspector is filled in with Data by calling that objects Constructor. This gives you all default values and Constructor values and allows them to be inspected.

The problem arises when you try to GetComponent or Use a different resources(accessing the Scene or a Manager of some kind), because the Inspector can be filled at any time even when the game is not running in editor. The will start causing undefined behaviour and can lead to some very strange and difficult bugs and errors.

This can also occur I believe with objects tagged System.Serializable (so they show in the Inspector), but if it’s not viewable in an Inspector than you can use Constructors however you desire.

Thanks for the explanation Ntero.

That would explain some of the random crashes I get sometimes…I think I might have a class or 2 with constructors which is shown in the inspector, and now that I know a little more about it - thanks to you - I understand why I have been running into a few hicups.

On the other hand, I do use constructors in all my non-Monobehavior classes - like the one above - so I’m glad to hear that it won’t be a problem.

Thx again,
Stephane