I’m having a non-static class with some static elements. I have a static float array [6, 6], and I want to initialize it on awake, and then just use it, but since my class is non-static - every time I create an object, with this script in it - the array values will be re-assigned (even if they remain the same).
I tried to make a static void Awake/Start, but that didn’t run at all.
You can use a static constructor.
“A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.”
Note that you have no control of exactly when the static constructor is executed and it can be different for each platform.
class MyClass
{
static MyClass()
{
// do initialisation of static fields here
}
}
5 Likes
That worked! Thanks ![]()