# How to initialize once for all a static field of a custom editor in unity

I have a custom editor wich has a static field.

Suppose you have a static field of type MyObject:

public class myEditor : Editor {

...

private static MyObject myObj;

...
}


I'd like to initialize the field myObj directly on the same line of the declaration, but in this way I get a TypeInitializationException.

I could initialize the field myObj in the method OnEnable, but in this way I have to use an if statement like the following:

if ( myObj == null )
myObj = new MyObject();


Is there a way to initialize myObj once for all without using if statement?

• Is this the code that you are using? I ask because it looks like field shouldn't be there and would cause a compilation error. – Kelly Thomas Dec 9 '14 at 12:29
• Of course no, sorry now I edit :) – optimusfrenk Dec 9 '14 at 13:08
• Have you examined the TypeInitializationException.InnerException property? – Kelly Thomas Dec 9 '14 at 15:03

Static myEditor(){