You need a pattern known as “set & invalidate.”
For this you either need a special value or a special flag.
You went with a special flag (PositionSet
), though because you can work with null
you get a special value for free. I.e. PlayerExitPos
is all you need. It’s de facto not set when it’s null
, and set when it’s not null
.
A drawback of “set & invalidate” is that you need to make sure the value is ‘invalidated’ at proper times, otherwise it is forever locked, which is the opposite of what is happening to you.
That said, you are actually implementing this pattern, and you use PositionSet to know the current state. However, the code that is faulty is not shown. I’m guessing you’re invalidating too early somewhere. Try to find where exactly you are setting PositionSet
back to false
and try to reason with the whole picture.
Debug in places to detect how exactly PositionSet
evolves over time.
Other than that, this code isn’t particularly bad (if we ignore the fact it bit your ass already
).
This paragraph is FYI (only topically related to your problem)
In general, because this is a very frequent thing, when you’re working with nullable types you can (and should) always rely on null
as a special value, without introducing new bool
variables. When you’re working with non-nullable types (likely primitive values and structs), you can use C# Nullable<>
types. For example, a Nullable<int>
is aliased as int?
and this is how you work with this
int? a = null;
int? b = 25;
if(!a.HasValue) a = 17;
if(a.HasValue && b.HasValue) {
a = a.Value + b.Value;
b = null;
}
Debug.Log($"a is now {a}");
Debug.Log($"b is now {b}");
When working with geometry, you can invalidate a Vector3 (for example) by assigning float.NaN
(‘not a number’ flag) to it’s components. The positive side-effect of this is that NaNs propagate like zombies when they’re evaluated in an expression, and they don’t produce hard errors (i.e. drawing a line would likely not draw a line if it handles NaN correctly). This is also the negative side-effect of using them, so take this with a grain of salt.