Does destroying object at List element make it null?

I'm using a list in Unity and I'm adding objects to it as they are collected through the game. But they will then be used and destroyed. My first question is do I use Destroy(), or should I just use List.Remove()? I'm not sure what happens to an object once it is removed from a list and not used anymore. Does garbage collection get it? My other question is, when I check the list (I will always be checking the first index), if that object was just destroyed or removed, will that element now be null? I've tried reading about this but it's hard to find an exact answer. Thanks for the help.

For reference, here is the code that I came up with based on my current assumptions about lists:

public List<BasePickUp> pickUpQueue = new List<BasePickUp>();
public bool pickUpActive = false;

void Update ()
{
    if (pickUpQueue[0] != null && !pickUpActive) 
    {
        pickUpActive = true;
        pickUpQueue[0].ActivatePickUp();
    }   
}

Edit: I have been playing with the if statement conditions, and even if I change pickUpActive to true, it somehow still manages to get inside the if statement to the breakpoint there. How is that possible? The condition says pickUpActive must be false, and yet it blows right by it. When I set the first condition to pickUpQueue != null, I get an error for index out of range once, when it is set to check the first element, as it is in the above code, I get an index out of range every frame. I finally changed it to pickUpQueue.Count != 0, and that finally kept it from getting into the if statement. If anyone can help me understand what is going on with my bool, and to understand exactly how to handle this list, I would greatly appreciate it! Thanks!

Answers 1

  • I'm using a list in Unity and I'm adding objects to it as they are collected through the game. But they will then be used and destroyed. My first question is do I use Destroy(), or should I just use List.Remove()? I'm not sure what happens to an object once it is removed from a list and not used anymore. Does garbage collection get it?

    You should use Destroy to destroy objects. Removing from an object from a list does not destroy the object. For example, let's say you create 10 Ball Objects into the scene and add them to a list. If you remove these objects from the list, they will still be visible in the scene. Object destruction and garbage collection will only occur on objects that you've destroyed.


    My other question is, when I check the list (I will always be checking the first index), if that object was just destroyed or removed, will that element now be null?

    If an object was just destroyed, assuming it inherits from UnityEngine.Object (e.g. like a MonoBehaviour), that element in the list will now be null. Removing the object from the list is a separate step.

    The reason the element becomes null once you destroy it is because Unity overloads the == operator to return null on destroyed UnityEngine.Object objects. You can find more details about this custom null check at this Unity blog post: "Custom == operator, should we keep it? ". There are reasons behind this, but as mentioned there it is counterintuitive.


    EDIT: For the comment in your edit, I suggest you use the debugger to try to figure out what's going on with your code.


Related Questions