How and When to use Constructor In Unity


Well my question may be a bit general , but I was wondering about what can be the benefit or not, to use class that does not inherit from monobehaviour and then use their own constructor instead of using Awake() for example to do initialization.

So to try to summarize a bit >>

  • What is the difference in term of overall performance for the program to have class not using monobehaviour , if there is any..

  • When you should prefer class that does not inherit from monobeahviour

  • Should I care about that at all , once again for overall performance or memory..for an iPhone game for example..

Well I never use ctor right now in my unity game, only once trying convert some XNA code to unity , but finally get rid of ctor way to something more practical to work with unity at the end and so my question is :)

Thanks for your time by advance

If you want to put a script on a GameObject, then the class has to inherit from the monobehaviour. You can have helper classes that don't in the same file though. Since Awake is a member of monobehaviour, if you want to use that, then you have to inherit from the mb. Otherwise, if you are just making a helper class of sorts, then, it doesn't make sense to inherit from the monobehaviour. Then you can use the constructor. I really don't know about the performance of the constructor vs Awake, but the constructor often gives strange results on a class that inherits from the monobehaviour.

I would have to say, if you don't need to inherit from the monobehaviour then don't. It would be like having all your classes implement ICodeGenerator just because you can. Well, there not exactly the same, but you get the idea. Implementing some class or interface you won't use just doesn't make sense.

Also, I haven't noticed any real performance changes.

Hope that helps.

Peter G.