On Save... How does ItemDropper know when something has been picked up

I’m having trouble understanding how the ItemDropper script knows when an item has been picked up. I can see that the Pickup script destroys the gameobject, but how does that make the item in ItemDropper’s pickup List null? Is ItemDropper scanning the Hierarchy when RemoveDestroyedDrops() is called?

It doesn’t appear to do so. It seems to be searching through the droppedItems List, but if droppedItems is only updated when an item is dropped, how could a part of it be null?

It’s part of the magic of C#. We’re taking advantage of a C# feature that does not work in many other languages…
When we pick up the item, the pickup is indeed destroyed. Once an object is destroyed,any reference to that object automatically becomes invalid, and when it is referenced, it turns up as null. This allows us to use RemoveDestroyedDrops() to prune the list of invalid objects.

2 Likes

Okay. That’s interesting that it’s a C# feature. That’s like the null reference exceptions, right? Except in this case, we are checking to see if it is null. Neat! I can see a lot of uses for this. Thanks!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms