Error: ArgumentException: You have already claimed the path with that object (CarModelo_1 (FollowPathCars)). Are you claiming the path with the same object twice?

Hi all! I’m getting the following error when you press play:

ArgumentException: You have already claimed the path with that object (CarModelo_1 (FollowPathCars)). Are you claiming the path with the same object twice? Pathfinding.Path.Claim (System.Object o) (at Assets/AstarPathfindingProject/Core/Path.cs:535) FollowPathCars.OnPathComplete (Pathfinding.Path _p) (at Assets/AstarPathfindingProject/Core/AI/FollowPathCars.cs:297) Seeker.OnPathComplete (Pathfinding.Path p, Boolean runModifiers, Boolean sendCallbacks) (at Assets/AstarPathfindingProject/Core/AI/Seeker.cs:329) Seeker.OnPathComplete (Pathfinding.Path p) (at Assets/AstarPathfindingProject/Core/AI/Seeker.cs:286) Pathfinding.Path.ReturnPath () (at Assets/AstarPathfindingProject/Core/Path.cs:698) AstarPath.ReturnPaths (Boolean timeSlice) (at Assets/AstarPathfindingProject/Core/AstarPath.cs:2400) AstarPath.Update () (at Assets/AstarPathfindingProject/Core/AstarPath.cs:892)

It seems that does not affect performance, but do not understand why it happens.
What I’m trying to do in my code is this: I make an enemy Vector3 direction from the player. (As if the field of vision of the enemy).
When the visual field is lost (ie the raycast I run with the direction of the enemy to the player collides with an obstacle), that’s when I want the pathfinding generate a path to follow the player until there again “Visual field”. Since visual field when I want the enemy to follow player for an own script that has different behaviors …

I took the script “AIPath.cs” and I’ve changed a little. I’ve created a function with this code to each frame to detect if the visual field is lost:

Vector3 vec = new Vector3(0, 0.5f, 0); ray = new Ray(transform.position+ vec, target.transform.position - transform.position); //Debug.DrawRay(transform.position + vec,/* vec +*/ (target.transform.position - transform.position), Color.red); if(Physics.Raycast(ray, out hit, Vector3.Distance(transform.position, target.transform.position))) { //Si el campo visual se corta generamos una ruta para seguirla. En caso contrario desactivamos la ruta if (hit.collider.gameObject.layer == 14) { OnEnable(); CalculateVelocity(GetFeetPosition()); startHasRun = false; //Debug.Log("SIN CAMPO VISUAL"); } else { OnDisable(); startHasRun = true; //Debug.Log("CON CAMPO VISUAL"); } }

And this is the code that generates the error me, but I do not understand why, or how to fix it without having to change what I want to do …
Can anyone help please?
Excuse me for my bad english
Greetings and thank you

Hi

Your code is causing the path pooling code to get all confused.
You can read more about path pooling here: http://arongranberg.com/astar/docs/pooling.php

It most likely has to do with your calls to OnEnable and OnDisable. If you want to enable or disable a component, don’t call the OnEnable and OnDisable methods, instead use the ‘enabled’ property.

See https://unity3d.com/learn/tutorials/modules/beginner/scripting/enabling-disabling-components
and http://docs.unity3d.com/ScriptReference/Behaviour-enabled.html