Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Destroy error

Discussion in 'Scripting' started by newagear, Aug 19, 2019.

  1. newagear

    newagear

    Joined:
    Aug 7, 2019
    Posts:
    18
    Hi !This piece of code is supposed to destroy 4 scripts.It only destroys the first 2 and the last one.
    Destroy(GetComponent<movement>());
    Destroy(GetComponent<fire>());
    Destroy(GetComponent<camera_follow>());
    Destroy(this);
    What is wrong?
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Are you getting any console errors? Are you certain the GetComponent calls are all returning the expected components and none are returning null?
     
  3. newagear

    newagear

    Joined:
    Aug 7, 2019
    Posts:
    18
  4. newagear

    newagear

    Joined:
    Aug 7, 2019
    Posts:
    18
    Is it possible due to execution time?(it destroy the first 2 scripts and then the camera script execute(it is lateupdate))
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Destroy only schedules something to be destroyed before the next frame. It does not destroy anything immediately. Everything you call destroy on will complete its execution of the current Update loop before actually being destroyed.

    https://docs.unity3d.com/ScriptReference/Object.Destroy.html

    If you are expecting that these components are destroyed as soon as you call Destroy, then that is probably the problem.
     
  6. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536

    There is
    'DestroyImmediate' (or whatever it's called) but it's not recommended to use it.


    are you sure? how did you test it?
     
    Joe-Censored likes this.
  7. newagear

    newagear

    Joined:
    Aug 7, 2019
    Posts:
    18
    I except that the components will be destroyed before the curent frame ends.But seconds after , they are still active
     
  8. Give us real code to check your logic. You can be sure that the Destroy command works properly.
     
    Joe-Censored likes this.
  9. newagear

    newagear

    Joined:
    Aug 7, 2019
    Posts:
    18
    The code , including the other classes has well over 300 lines of code.
     
  10. Well, for started, your four lines above are completely wrong, you don't need to destroy the components of you're destroying your game object. (Unless your code above is different what you're doing in your real code. But if you do, don't expect us to be able to help, since we don't magically see what you're doing over the internet...)

    Well, you can debug your code, step by step, what is happening when you destroy your game object/components. You can check if you're seeing what you think you're seeing. And not another game object with similar set of components, etc.
    Or you can put in the effort and at least guess the relevant pieces of your code and post it here to able to us to help you out.
     
    Joe-Censored likes this.
  11. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    I mixed those up too when I first read the thread here, he's destroying 'this', not 'gameObject'.

    Also, I agree with ninja about posting the full code(or at least the full function) 300 lines is nothing, lol.
     
    Lurking-Ninja likes this.
  12. newagear

    newagear

    Joined:
    Aug 7, 2019
    Posts:
    18
    I had programs in cpp that had 4 thousands lines of code , and unbelievable they worked FLAWLESS.I believe I will just use script.enabled.Sometimes it shuts down all of them , sometimes it shuts down one of the scripts.
     
  13. YetAnotherKen

    YetAnotherKen

    Joined:
    Jun 17, 2019
    Posts:
    30
    I think the problem is related to the delay between when you flag the object for destruction and when the garbage collector actually deallocates the object's used memory. I suspect that one way around this is to use the object pool idea. When you no longer need the object return it to the pool of inactive objects. When you need another you pull it from the pool. This way you never really have to worry about the timing of the garbage collection calls.