Hi! I am making a grid based strategy game, and when I select a unit, I want to calculate how long each path is in a certain area (hard to explain in words). However, I am getting an error: Canceled path because a new one was requested.
Here is my code that is causing the error:
foreach(GameObject i in SelectedTiles){
Vector3 testTo = i.transform.position;
distanceTester.StartPath (transform.position, testTo, AfterTest);
}
Basically, for each object in the list Selected Tiles, I need to calculate a path to it, and then use .GetTotalLength() to find out how long it is. If it is larger than a certain length, it will be removed from the list of objects.
The Seeker component is designed to be used for a single agent, and since agents only follow a single path at a time, it will cancel the previous path calculation if a new one is started. If you want to calculate multiple paths simultaneously you can bypass the Seeker by calling AstarPath.StartPath instead.
foreach(GameObject i in SelectedTiles){
Vector3 testTo = i.transform.position;
var path = ABPath.Construct(transform.position, testTo, AfterTest);
AstarPath.StartPath (path);
}
Okay, it almost works now I think, but I’m having one error. I created two lists, tempSelectedTiles, and SelectedTiles, the foreach loop checks the length of the path to tempSelectedTiles and if it is within range, add the gameobject to Sekected Tiles (this might not be the most efficient way to do this).
My code looks like this:
foreach(GameObject i in TempSelectedTiles){
Vector3 testTo = i.transform.position;
var path = ABPath.Construct(transform.position, testTo, (p) => {
if (p.GetTotalLength() <= maxRange) {
SelectedTiles.Add(i);
ActionVis tileAV = i.GetComponent<ActionVis>();
tileAV.turnOn();
}
});
AstarPath.StartPath (path);
}
However, this is only effecting the first tile that is calculated, not each tile in TempSelectedTiles. Any idea why this would be? It is only executing tileAV.turnOn() on the tileAV component of the first gameobject.