1
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ I did use the debugger, but it's not giving me much to go on. The list count shows 0 but it's not null? I'm confused on that part. It sounds like what I should do then is check if the list count is not equal to 0. And then when I am done with an item, I should destroy the object and then remove the element? If there is 1 element and I do this process, then the list count will be equal to 0, correct? \$\endgroup\$ – ethan codes Jun 7 '18 at 13:05
  • 1
    \$\begingroup\$ @ethancodes #1. list count can be 0 but not null if it's been created but there are no elements in it. The line public List<BasePickUp> pickUpQueue = new List<BasePickup>; in your example makes pickUpQueue have a reference and not be null, with an initial count of 0 because there is nothing in the list. If you just declared public List<BasePickUp> pickUpQueue;, then the list would be null because you haven't set it to anything. #2. Yes, you should destroy and remove the element when you are done with it. #3. Yes, if there is 1 element and you remove one, the list count will equal 0. \$\endgroup\$ – sonny Jun 7 '18 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.