Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
    
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 :) –  frenk Dec 9 '14 at 13:08
    
Have you examined the TypeInitializationException.InnerException property? –  Kelly Thomas Dec 9 '14 at 15:03

1 Answer 1

up vote 0 down vote accepted

You could use a static constuctor

Static myEditor(){
    myObj = new MyObject();
}

This will run the first time you access the class

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.