Hi to everyone !!! I'm trying to make a DEBUG MODE for my project in which I created a static C# class that provides an ArrayList (to store the informations I need to display in a Debug window) and some others accessories functions. Pratically I'm trying to make my own "Stats" window. Here's an example of how to use the static class: Code (csharp): using UnityEngine; using System.Collections; public class AnyClassOfProject : MonoBehaviour, ... { . . . // all the debug value to show if DEBUG MODE is enabled private DebugData dd1; private DebugData dd2; private DebugData dd3; . . . ~AnyClassOfProject () { if (DebugManager.DEBUG_ENABLED) DebugManager.removeDebugData (dd1); DebugManager.removeDebugData (dd2); DebugManager.removeDebugData (dd3); . . . } // if } // destructor void Start () { . . . if (DebugManager.DEBUG_ENABLED) { dd1 = DebugManager.createDebugData ("string1 to show in DebugWindow"); dd2 = DebugManager.createDebugData ("string2 to show in DebugWindow"); dd3 = DebugManager.createDebugData ("string3 to show in DebugWindow"); . . . } // if } // Start void Update () { . . . if (DebugManager.DEBUG_ENABLED) { dd1.setDataValue ("the value of data1 to show"); dd2.setDataValue ("the value of data2 to show"); dd3.setDataValue ("the value of data3 to show"); . . . } // if } // Update . . . } // AnyClassOfProject The problem is that the destructor of this class doesn't seem to call the static method of the static class to remove the information from the ArrayList. To tell you the thruth I've also tried to check the if (in whitch I try to check a static boolean value of the static class) and i found that the destructor executes the external instruction but not the ones in that if (it seems it cannot check the static variable of the static class). Just to be precise, I also tried to make all the deletions out the if but it doesn't work. Please, can someone help me to understand what's wrong? If it can help, I can also post the static class... just ask me. THANKS A LOT GUYS !!! :wink:
I presume you might need to not have this static but instead have it as a regular class object and instansiate it. As far as I am aware (and please someone knock me out if I am wrong), once the program starts, the static class is never destroyed until the program is terminated. Have a static array that stores elements generated by the debug class. So say you instansiate the class in a component, then fill a static array with elements generated by that class object. When the component gets destroyed, the destructor will trigger. HTH or let me know if I am way off in what you are trying to do...
Here's a shot in the dark: Is the destructor actually being called? Have you put Debug.Log("I am being destroyed dude!"); in the destructor? The reason I ask is: An object is eligible for destruction when it is no longer possible for any code to use the instance... Well your static class can use the instance (the DebugData objects). Are you sure it is being destroyed?!? I think you are right about the static class but it's not the static class he's having problems with. Ta JT
You shouldn't use a constructor or destructor with any class derived from MonoBehaviour as those functions will be called at unexpected times. In the editor, this can even happen while the game isn't playing. The constructor is generally replaced with Start or Awake, and the destructor can be replaced with OnDisable. OnDisable fires when a script's .enabled member is set to false, but it also fires when the object is destroyed, so it's useful for this purpose. You can also use OnApplicationQuit if you just want to clean up at the end of the game.
I don't see why you'd want to do manual cleanup here though? You've got the garbage collector doing that for you.
Because I've an ArrayList that store the pointer of all the DataDebug object of the project... so... if i don't remove this pointer from the ArrayList they will remain in it and they will be displayed in the Debug window even if they're no more usefull !!! :wink: However, I'll try some of the NCarter proposals solutions and I'll let you know !!! THANKS to everyone.
Ok guys... to make that piece of code works you simply need to change this function Code (csharp): ~AnyClassOfProject () { if (DebugManager.DEBUG_ENABLED) { DebugManager.removeDebugData (dd1); DebugManager.removeDebugData (dd2); DebugManager.removeDebugData (dd3); . . . } // if } // destructor with this one Code (csharp): public void OnDisable () { if (DebugManager.DEBUG_ENABLED) { DebugManager.removeDebugData (dd1); DebugManager.removeDebugData (dd2); DebugManager.removeDebugData (dd3); . . . } // if } // OnDisable :wink: I think that this will work only if the class exends MonoBehaviour... but what if it doesn't? I make all this to have the possibilities to check the varibles values that are not possible to check in the inspector Debug view: some classes of my project are not attached to a game object so I've to use Debug.Log(string) funcion. That's not so comfortable if you cannot move your eyes from the scene to check what will be the game objects behaviour; so I create a window that is able to show in realtime what I need to know (e.g. the FPS rate, some boolean flags value, etc.).