- Home /
OnDestroy-like function for non monobehaviour
Hello everyone, Im wondering if there's a way to mimic the OnDestroy function on monobehaviour for regular class. for example
i want to count how many instance of this class.
public class MyClass {
public static int MyClassCounter;
public MyClass() {
MyClassCounter++;
}
public OnDestroy() {
// will be called when Garbage Collector collects it ?
MyClassCounter--;
}
}
Thank you. :)
Comment
Answer by Tseng · Oct 08, 2011 at 05:18 PM
You could try implementing IDisposable interface.
public class MyClass : IDisposable {
public static int MyClassCounter;
public MyClass() {
MyClassCounter++;
}
public void Dispose() {
// release unmanaged resources, if any
MyClassCounter--;
}
}
It should be called on garbage collection.
Your answer
Follow this Question
Related Questions
How to check if OnDestroy has finished? 1 Answer
How to destroy Scriptable Objects on Reimport 1 Answer
OnEnable/Disable on property drawers 3 Answers
switch weapon onDestroy 1 Answer
Some objects were not cleaned up when closing the scene 2 Answers