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