Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Destroy() complications

Discussion in 'Scripting' started by VictorKs, May 28, 2018.

  1. VictorKs

    VictorKs

    Joined:
    Jun 2, 2013
    Posts:
    242
    I am developing an rts game where a unit is killed when HP==0. So my problem is that when I call Destroy() the unit is destroyed on the end of the frame and I fear that in this period between Destroy() and actual object destruction I may get some bad interaction.

    A unit can interact:
    1)by player clicking on it;
    2)with OnTriggerEnter() with another unit, where a UI window opens.

    How can I be sure that such interaction is imposible in that gap between Destroy() and actual destruction??
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Well, you could just DestroyImmediate() instead.
     
    VictorKs likes this.
  3. VictorKs

    VictorKs

    Joined:
    Jun 2, 2013
    Posts:
    242
    I tried it and while it works documentation suggests against using DestroyImmediate(). I also read somewhere in the forums that it is used for editor code, I have no clue though as to why we should avoid it.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Hmm, you're right, the docs are rather grumpy on the topic. I think they're exaggerating. I've used DestroyImmediate and have never seen any reason to believe that it is buggy or dangerous, beyond the obvious issue that it destroys the object immediately, so any references you have to it will become invalid.

    But anyway, the alternative is to put a flag on your object marking it as condemned, and check that flag before you do anything with it in those interaction routines.
     
    VictorKs and Owen-Reynolds like this.
  5. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,914
    And in practice, you may want destroyed objects to exist for a second or two during a death animation. Then you really have to add a dead flag, or an "isDead()" test, or remove it from the list of alive units. Any "yes I can see it, but is it alive?" check.

    You'd think you'd destroy it and replace it with a dead prefab, but you may have particle systems coming from it, or arms/legs/turrets at funny angles and so on. Replicating that in the dead version is a pain (and having a "snap" from alive to dead only looks cool the first 20 times.) I've even used the original to die, then removed the script, colliders and rigidbody, changed the layers, to turn it into rubble.
     
    VictorKs and JoeStrout like this.
  6. VictorKs

    VictorKs

    Joined:
    Jun 2, 2013
    Posts:
    242
    Ok I used a flag so a dead unit can no longer interact with the rest of the game. Thanks for the help!
     
    Joe-Censored likes this.