Search Unity

Destroy command not working

Discussion in 'Scripting' started by kubaak502, Jan 18, 2021.

  1. kubaak502

    kubaak502

    Joined:
    Jan 18, 2021
    Posts:
    5
    if (Przeszkoda.transform.position.z <= -5f)
    {
    Destroy(Przeszkoda);
    }
    This is a fragment of my code, why doesn't it destroy the "przeszkoda" objects when they pass the location -5 on the z axis. I don't get any errors, it just doesn't work.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    Keep in mind that Destroy() does not happen immediately but rather at the end of the frame. Usually this does not matter, but it might depending on your reliance on that value.

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
    kubaak502 and Joe-Censored like this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,908
    Another thing to note... what kind of thing is "Przeszkoda"? Is it a GameObject, or a Component? If it's a Component (for example one of your scripts), Destroy will only destroy that individual component and not the entire GameObject.

    If you want to destroy the whole GameObject you need to do
    Destroy(Przeszkoda.gameObject)
     
    kubaak502 and Kurt-Dekker like this.
  4. kubaak502

    kubaak502

    Joined:
    Jan 18, 2021
    Posts:
    5
    You're right it for some reason does not trigger at all. Why could that be. Is there something wrong with the if? Bcs Przeszkoda is my prefab obstacle in my game.
     
  5. kubaak502

    kubaak502

    Joined:
    Jan 18, 2021
    Posts:
    5
    Przeszkoda is my prefab obstacle but apparently the code never triggers
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,908
    Is it the prefab, or a clone?
     
    Pagefile and kubaak502 like this.
  7. kubaak502

    kubaak502

    Joined:
    Jan 18, 2021
    Posts:
    5
    It is a prefab that gets cloned a lot, I want to destroy objects that go off screen so they don't take up memory.
     
  8. Pagefile

    Pagefile

    Joined:
    Feb 10, 2013
    Posts:
    51
    Is this if inside Updat()?
     
  9. kubaak502

    kubaak502

    Joined:
    Jan 18, 2021
    Posts:
    5
    yes
     
  10. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    You must refer to the instantiated object, instead of the prefab itself. When you Instantiate the prefab, save it at some variable in order to be able to come back to it and destroy it. Btw Unity has a method called OnBecameInvisible which could be usefull for this purposes.
     
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    If something is a prefab, it is on disk. You cannot and you do not destroy it at runtime. Just stop referencing it and eventually the memory will be released.

    In fact if you try to Destroy() a prefab on disk, you should see a warning or error in the console.

    If something is in-scene and you are using it as a template to clone fresh ones, you may delete that.

    If you make a copy of something with Instantiate, that function returns a reference that you may later Destroy().