Static attributes defined in static constructor are null

public class AnimatorConstants {

    public static AnimatorParameter Jumping;
    public static AnimatorParameter Grounded;
    public static AnimatorParameter Running;
    public static AnimatorParameter Dashing;

    static AnimatorConstants()
    {
        Jumping = new AnimatorParameter("Jumping", AnimatorParameter.Type.Bool);
        Grounded = new AnimatorParameter("Grounded", AnimatorParameter.Type.Bool);
        Running = new AnimatorParameter("Running", AnimatorParameter.Type.Bool);
        Dashing = new AnimatorParameter("Dashing", AnimatorParameter.Type.Bool);
    }
}

Why won’t this work? In another script, I try to use AnimatorConstants.Dashing.Name but this is equal to Null. I’ve also tried have the class inherit from MonoBehavior and instantiate the objects in Awake() method but that didn’t work.

Strange this suppose to work, I know that, I use.

Try to transform this constructor into a private static function then decorete it with RuntimeInitializeOnLoadMethod see in here Script Ref, this is guaranteed to run before ant Awake function on the scene. Also try passing RuntimeInitializeLoadType.BeforeSceneLoad to the attribute if does not work.


The vantage of doing this is that you can call unity functions like Animator.StringToHash (That you maybe calling inside of the AnimatorParameter constructor).

You can also call Resources.Load, SceneMananger.Load … you get the idea. Right?


Edit: static constructors and MonoBehaviours constructions only works for code that is not related to unity’s functions, except by Mathf, Vector’s, Matricies. It could have more, the one way of telling is by calling them and looking at the console.

I was using the shorthand getter and setter to make a property for two attributes in AnimatorParameter when I should have defined the getter and setter myself.

Before (caused Null to be returned when accessingName as property):

public class AnimatorParameter
{

    public enum Type : byte
    {
        Float, Int, Bool, Trigger
    }

    /// <summary>
    /// Animator Controller Parameter Name
    /// </summary>
    private string name;
    public string Name { get; private set; }
    
    /// <summary>
    /// Animator Controller Parameter Type
    /// </summary>
    private Type parameterType;
    public Type ParameterType { get; private set; }

    public AnimatorParameter(string name, Type type)
    {
        this.name = name;
        parameterType = type;
    }
}

After (contains changes that solved my problem):

public class AnimatorParameter
{

    public enum Type : byte
    {
        Float, Int, Bool, Trigger
    }

    /// <summary>
    /// Animator Controller Parameter Name
    /// </summary>
    private string name;
    public string Name
    {
        get { return name; }
        private set { name = value; }
    }
    
    /// <summary>
    /// Animator Controller Parameter Type
    /// </summary>
    private Type parameterType;
    public Type ParameterType
    {
        get { return parameterType; }
        private set { parameterType = value; }
    }

    public AnimatorParameter(string name, Type type)
    {
        this.name = name;
        parameterType = type;
    }
}

I can initialize the static attributes in AnimatorConstants as I declare them or in the static constructor or in the static method with that one decorator.