[UnityScript] What is the correct way to release resources when garbage collected?

In a UnityScript class which is NOT derived from anything in the Unity object hierarchy (i.e. this is not a ScriptableObject or MonoBehavior), what is the correct way to release resources when an object is garbage collected?

This code:

class Foo
    void Finalize() {//release resources here}

produces the following warning:

Overriding 'object.Finalize()' is bad practice. You should use destructor syntax instead.

however, I can not find the "correct practice" or "destructor syntax" for UnityScript in the documentation. The following just results in a syntax error:

class Foo
    ~Foo() {}

You could use on OnDisable().

This is also called when the object is destroyed and can be used for any cleanup code. When scripts are reloaded after compilation has finished, OnDisable will be called, followed by an OnEnable after the script has been loaded.

Eric5h5 posted a method of differentiating when destroyed instead of disabled.



whoops missed the whole not derived from MonoBehaviour. I'll think on it.

Does anyone know the answer to this? Thanks :slight_smile:

I am stuck in the same problem and need help.I find that the desctrucors will be called in another thread and well.But if I put some codes to clear the resources within the game into the desctructor then throw err.And everytime i press the play mode button, it show that desctructor be called first than constructor,I have no idea for it.does somebody have some suggestion and thanks:)