Switching to new state in State Machine. What happens to old state?

In the 3rd person traversal course we are creating new state every time we… well change state of a player in statemachine. So for example we are in FreeLookState, and we switch to TargetingState by creating a new TargetingState. Than we go back to new FreeLookState.

What happens to that “old” FreeLookState", that is now not used anywhere? (And to that TargetingState we just left.) Is it just cleaned by Garbage Collector? (I guess if this is true, answer to question how GC knows we are not using given instance anymore is a little too complicated and possibly goes too deep into C# itself. If it is so, omit this question.) Or am I missing some fundamental point here?

Yes, it’s being cleaned up by the garbage collector. The GC is a complex system, but it will essentially check the objects and if there are no more references to it (like the old state is once we replaced it), it will clean it up.

1 Like

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

Privacy & Terms