Fail to initiate static variables in c# of Unity

unity static constructor
c# initialize static variable
unity static start
c# initialize static class
unity static class
unity3d initialize on load
unity static class vs singleton
unity static method

I was trying to make a singleton in Unity 2017.

public class Singleton {

    public static readonly Singleton instance = new Singleton();

    static readonly float FLOAT_VAL = 3.5f;
    static readonly int INT_VAL = 3;

    private Singleton() {
        Debug.Log("FLOAT_VAL = " + FLOAT_VAL);
        Debug.Log("INT_VAL = " + INT_VAL);
    }
}

I expected to see the output of "FLOAT_VAL = 3.5" and "INT_VAL = 3". However, it came out with the output of "FLOAT_VAL = 0" and "INT_VAL = 0". Then I deleted both "readonly" modifiers, leaving "static" and still got two zero.

What happened to the initiation or construction of this singleton? How can I correctly give the values to the static variables? Is there anything to do with Unity? (I chose il2cpp when building the project, but this was tested under Editor Mode)

By the way, I figured out that the initiation of the variable "instance" didn't start until the first call (I used to think that all static vars get initialized when the assembly is loaded.). Is it also another Unity feature or just normal?

The fields get initialized in the order they are declared.... so since your instance comes first, your other values are zero. If you move them above your instance they will have the values you expect

Best way to initialize static class not attached to a , Best way to initialize static class not attached to a GameObject I actually used to use those in Java, but I didn't know C# supported them too. then you should be fine; the editor carries these values correctly into the Play know that this code will not get called unless you have the constructor like above. This post explains how to use static variables when programming scripts for the Unity3D engine. The example scripts in this post are written in C#, but the same guidelines apply for JavaScript. Before delving deeper into the subject, it goes without saying that static variables should be avoided at all costs,

From the C# language specification

§10.5.5.1

Static field initialization

The static field variable initializers of a class correspond to a sequence of assignments that are executed in the textual order in which they appear in the class declaration. If a static constructor (§10.12) exists in the class, execution of the static field initializers occurs immediately prior to executing that static constructor.Otherwise, the static field initializers are executed at an implementation-dependent time prior to the first use of a static field of that class.

So basically, because you wrote instance first, it is initialised before FLOAT_VAL and INT_VAL. Reverse the order and you will see your expected results:

public class Singleton {


    static readonly float FLOAT_VAL = 3.5f;
    static readonly int INT_VAL = 3;


    public static readonly Singleton instance = new Singleton();

    private Singleton() {
        Console.WriteLine("FLOAT_VAL = " + FLOAT_VAL);
        Console.WriteLine("INT_VAL = " + INT_VAL);
    }
}

My static variable is not changing, I'm just trying a simple test to get my static variable to change. Instantiate(​P_Death,transform.position,transform.rotation); I have not done much with C# so that doesn't much, but it seems like this should not be this complicated, so I'll look more into it Global Variables Refuse to Cooperate 1 Answer. Learn how to create static variables, methods, and classes. COVID-19 Support: We’re providing all users three months of complimentary access to Unity Learn Premium.

If your class contains static fields, provide a static constructor that initializes them when the class is loaded.[MSDN]

Add a static constructor to initialize your static variables

referring static variable of existing script gives null, Also, other scripts access 'MenuController menuControllerRef' in Start() or OnEnable() so changing execution order did not help much as my  Submission failed. For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Help with static variables not being saved, Help with static variables not being saved Now I have a problem when I set a variable and read it again its ok, but when I set it and then go back to read it, its not set. Collections;; public class Setup : MonoBehaviour {; void Start () {; MyScript. C# Parent-SubClass set inherited properties help 1 Answer. Static constructors with this attribute are called when scripts in the project are recompiled (also known as a Domain Reload). This happens when Unity first loads your project, but also when Unity detects modifications to scripts (depending on your Auto Refresh preferences), and when you enter Play Mode (depending on your Play Mode configuration).

how to use public static variables?, I attached Data with C# scripts SaveSceneData.cs for Scene00 and you initialize the static variable with the name of the current scene. much, the problem's solved and I realised there's something I did not know about! Static Constructors (C# Programming Guide) 07/20/2015; 4 minutes to read +7; In this article. A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.

static instance of a class is null. not sure why, static instance of a class is null. not sure why Generic; using UnityEngine; but i can't without the static instance existing :P At minute 33:00 of the video he actually mentioned that he forgot to initialize the static variable and he added an Awake other scripts failing to access a static script 1 Answer. Static Variables in Unity3d. Where to use a static variable, how to use a static variable, how to access a static variable in other scripts? With a great example, we will learn how to use a static

Comments
  • just normal. This is how .Net works.
  • What you mean by "just normal"? The delayed initiation? Or the unexpected values?
  • Welcome to SO. You don't have to modify your question and say that it is solved. People will know it is solved when you accept answer and you did in this case.
  • Statics not initialising until the first reference is a normal C# thing. Personally, I use a lazy-loader pattern for singletons.
  • Yes, that works. I have already been told that initiations of the static come with no orders. Should realize the dependence in static will make a mess. Thanks.