- Home /
Class c = new Class() okay in class body?
Is this okay?
public class MyScript : MonoBehavior {
Class c = new Class();
}
I know that it's generally advised to put code that needs to be executed in the beginning in the Awake()/Start() functions, but this seems to result in the newly created class not being the one I can see in the inspector. I guess I'm asking if there are any weird side effects that could happen if the class initialization happens outside in the class body.
Answer by nathanielstevens · Aug 15, 2014 at 08:47 PM
Yep! As long as your instantiating a new object and not assigning it a reference.
If you were assigning a reference (connecting it to a GameObject or component inside of a scene) you would need to put it into an Awake or Start function in order to prevent it from attempting to assign the reference before the GameObject or component was initialized.