How to establish instantiated Object variables before OnEnable() ??

// I instantiate an object and set variables on it like this.

// ActivateApp is just a fancy way to instantiate and
// set what layer the object is on - Game is in 2D only. 
// for our purposes, ActivateApp = Instantuate()
GameObject thread = GameControl.ActivateApp (app.gameObject, new Vector3(x, y, 0));
PlaySmsDialogue dialogue = thread.GetComponent<PlaySmsDialogue> ();
    			dialogue.Sender = SmsSender;
    			dialogue.DialogueFile = FileName;
    			dialogue.Avatar = Avatar;
    			dialogue.Times = Times;

The problem is, I need to access the variable, DialogueFile in the OnEnable () but it’s not yet available. It’s null. It is available in Start(), so my guess, is the variable initialization just doesn’t happen before OnEnable (). This object is frequently enabled and disabled so I can’t rely on Start() as it’s only called once, and I need to call it everytime the object is enabled.

I did find this little trick, but it would require serious rewrite of my code. It’s amazing to me that there isn’t a way to simply do what I need to do without this crazy work around code.

Any help greatly appreciated… I CAN’T be the only one who has this problem.

Use a getter for the DialogueFile. Assuming that DialogueFile is of the type MyType, it would look like this:

//in PlaySmsDialogue:
private MyType _DialogueFile;
public MyType DialogueFile {
    get {
        if(_DialogueFile == null)
            //initialize _DialogueFile to whatever it needs to be here
        return _DialogueFile;

Also a tip: variables should not be capitalized; your variable should be named ‘dialogueFile’, not ‘DialogueFile’. This is to differentiate them from types - if you see something like this:


You know that Foo is a type, and Method1 is a static function, while bar is an object, and Method2 is non-static. If you capitalize both types and variables, there’s no way to see the difference without checking. It becomes especially confusing if you name a variable the same as it’s type.

Hi, DialogueFile is a Property with backing value. I read on MSDN that these should be capitalized?

So this is a new way of me thinking about this. I’ll reach backward and grab the value instead of passing it forward.