Why is there a need to set things to null?

I see Sam doing it a bunch later, with some nifty logic. I Googled and it gives me hits about garbage collection, but I don’t think that is why he’s doing it.

It seems to keep the code cleaner and implement some kind of checking?

There are multiple reasons to set an object reference to null.

In Fighter, the Update method first checks to make sure that the target is null before proceeding. This means that when the Fighter is no longer targeting an enemy, that the target should be set to null.

Additionally, the Garbage Collector will not dispose of any object which still has an active pointer pointing to it (to avoid null references). This means that in order for GC to work, you need to clear the reference by setting it to null.

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

Privacy & Terms