Position Constantly Being Updated. Only want to update Once.

So when the player leaves the enemy’s line of sight I want to get the last known position of the player.
PlayerExitPos is assigned OnTriggerExit for when the player exits line of sight. I want PlayerExitPos to only be assigned once then stop being assigned. Currently its constantly updating it so the Enemy is always following my player once they leave line of sight. Sorry if my code is bad I’m new & learning.

 private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.name == "Player")
        {
            if (!PositionSet)
            {
                PlayerExitPos = other.gameObject.transform;
                PositionSet = true;
            }

            Searching = true;
            print("Searching!");
        }
    }

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 :smile:).

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.

Thanks for your awesome reply! But it turned out my issue was not in that area of code. I have a bool that tells the AI if it can see the player. I forgot to tell it that it cant when I leave the line of sight. So it was doing a different piece of code that just followed me. Just a noob mistake. Thanks for you informative reply though!

1 Like