Search Unity

Object destruction event

Discussion in 'Scripting' started by NCarter, Apr 15, 2006.

  1. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    Is there any counterpart for the Start event which fires when the affected object is about to be destroyed, or is there any sensible way of getting this effect?

    At the moment, I'm handling this manually, by having my 'exploding object' class send a message to all scripts on the object, but that doesn't really help in the general sense because it's necessary to manually trigger the event.

    I tried using the destructor, but of course this doesn't work well, as the destructor fires at unexpected times (like when you're in the editor).
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    There is no such event at the moment. But it's a pretty good idea.
    I'll put it on my todo list.
     
  3. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    Bump. Any news on this?
     
  4. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    OnDisable has since been implemented to address this particular situation, although it does have the disadvantage that it fires for various other things as well as script destruction (such as, of course, being disabled).
     
  5. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    Ok would be nice if it got seperated out. UT is there a posibility that this will happen at some point?
     
  6. nafonso

    nafonso

    Joined:
    Aug 10, 2006
    Posts:
    377
    Don't know how exactly you are doing it, but you could emulate this behavior in the following way:

    Code (csharp):
    1. public MyClass : MonoBehaviour{
    2.     public void Destroy(){
    3.         // do something here...
    4.         Destroy( gameObject );
    5.     }
    6. }
    7.  
    8. // then when you want do destroy just get the component and destroy it
    9. ((MyClass)GetComponent(typeof(MyClass))).Destroy();
    10.  
    Regards,
    Afonso