what MonoBehaviour functions == C++ con / destructors?

Hi

I’m just starting out with Unity after having spent 14 years doing traditional game development in C / C++ and am finding it a bit odd.

In general, Unity is completely awesome and has vastly exceeded my expectations; but I’ve hit a stumbling block that has got me totally stumped / deeply freaked out.

In all likelihood, I’m just being a muppet and have missed the obvious stuff about this in the docs.

Anyway here’s my basic problem:

I have an object that (for whatever reason) I want to instance and attach as a child of another obejct.

I do this in Start(), which is where the tutorial stuff says to do this sort of thing, but from what I gather this would be fine in Awake() too.

When I run the game in Unity by pressing the “play” button, (as expected) the object gets cloned and attached as a child of the other object fine, it renders, it moves about as expected. So far so froody.

However, when I un-click “play” the cloned object stays there and I have to manually delete it from the hierarchy view.

I had a look at the docs, and it said OnEnable() and OnDisable() were for initialisation and shutdown, so I tried using them and they didn’t get called at all despite the fact the script is clearly initialising and updating :-/

I’ve even tried putting the clean up code for the cloned object into OnApplicationQuit() and that doesn’t get called either.

Argh :shock:

I mean the fundamental interface of any object that’s used for games is that it needs the following functions:

Create() - slow one-off stuff (allocate memory etc.)
Reset() - reset the object’s state to initial state (separated from Create() so operations such as restarting a level are fast)
Update( timestep ) - update function called once per frame
Destroy() - cleans up any system resources allocated by Create

Call me a stickler for details, but I quite like to have explicit control over how I initialise and shutdown objects…

So, is there any “opposite” function to Start()?

Or am I just missing some huge paradigm shift in perspective that is needed for Unity development?

cheers :slight_smile:

darbotron

OnEnable/OnDisable should actually work. They are called when scenes are loaded or when the object with the script is created/destroyed.

Yeah, I actually hadn’t done the obvious and just logged in the OnEnable / OnDisbale functions to check whether they were being called or not; I’ve just done it and it got weirder…

I’ve put logs to say “ called” in OnEnable, OnDisable, and Start for the script and it’s attached to just one object.

The output I got when I pressed the play button was:

OnDisable() called
OnEnable() Called
OnDisable() called
OnEnable() Called
Start() called

and then nothing when I pressed the play button again to stop it.

This struck me as a little odd, so I had a think.

I remembered I was using [ExecuteInEditMode] in my C# code (as the “build a platformer” tutorial said this was a handy way to see what was going on when you were in the Unity editor as well as when running the game).

Apparently this advice should have come with the caveat that it will TOTALLY change the behaviour of the script, and the OnEnable / OnDisable scoping of the script will go out of the window for actual game use.

Removing [ExecuteInEditMode] gives me the following output when I press play:
OnEnable() Called
Start() called

and when I press it again:
OnDisable() called

Win.

Thanks for the help - hope this post helps someone else :slight_smile:

darbotron