Ok, I got it running… but I am receiving one error that I am not sure how to fix…
Here is the situation: the AstarPath.active instance is only set in Awake(), so there is no chance for me to ever reactivate it OnEnable() …
The solution: whenever I cache my scene (deactivate my “scene” gameobject), I disable the “old” AstarPath instance in it, and create a new one that I use when the “scene” is re-activated… here is the code:
// deactivate the “scene” container, but before make a copy of the “old AstarPath”, and destroy the old one.
public void Deactivate()
{
var oldAstar = container.GetComponentInChildren<AstarPath>(true);
container.SetActive(false);
var newAstar = GameObject.Instantiate(oldAstar, oldAstar.transform.parent);
newAstar.gameObject.name = oldAstar.gameObject.name;
GameObject.Destroy(oldAstar.gameObject);
}
// coroutine that activates the “scene” container object, re-scans the graphs, and waits until scanning is finished. (loading screen will be hidden afterwards)
public IEnumerator Activate()
{
container.SetActive(true);
AstarPath.active.Scan();
do
yield return Game.Coroutines.WaitForEndOfFrame;
while (AstarPath.active.isScanning);
}
So far it works like a charm… BUT: I guess due to some Unity gameobject lifecycle shizzle, the “old AstarPath” gameobject is not destroyed until I re-open that scene container… remember, the cached “scene” is deactivated, so I guess Unity does not call OnDestroy() until the container is re-activated.
Which leads to this error message - which does nothing else but throw an error - the game itself seems to run fine… :
IndexOutOfRangeException: Index was outside the bounds of the array.
Pathfinding.HierarchicalGraph.CompactifyDirtyNodes () (at Library/PackageCache/com.arongranberg.astar@4.3.41/Core/Misc/HierarchicalGraph.cs:168)
Pathfinding.HierarchicalGraph.AddDirtyNode (Pathfinding.GraphNode node) (at Library/PackageCache/com.arongranberg.astar@4.3.41/Core/Misc/HierarchicalGraph.cs:190)
Pathfinding.MeshNode.ClearConnections (System.Boolean alsoReverse) (at Library/PackageCache/com.arongranberg.astar@4.3.41/Core/Nodes/GraphNode.cs:604)
Pathfinding.GraphNode.Destroy () (at Library/PackageCache/com.arongranberg.astar@4.3.41/Core/Nodes/GraphNode.cs:110)
Pathfinding.NavGraph+<>c.b__25_0 (Pathfinding.GraphNode node) (at Library/PackageCache/com.arongranberg.astar@4.3.41/Generators/Base.cs:291)
Pathfinding.NavmeshBase.GetNodes (System.Action`1[T] action) (at Library/PackageCache/com.arongranberg.astar@4.3.41/Generators/NavmeshBase.cs:331)
Pathfinding.NavGraph.DestroyAllNodes () (at Library/PackageCache/com.arongranberg.astar@4.3.41/Generators/Base.cs:291)
Pathfinding.NavGraph.OnDestroy () (at Library/PackageCache/com.arongranberg.astar@4.3.41/Generators/Base.cs:275)
Pathfinding.NavmeshBase.OnDestroy () (at Library/PackageCache/com.arongranberg.astar@4.3.41/Generators/NavmeshBase.cs:249)
Pathfinding.NavGraph.Pathfinding.IGraphInternals.OnDestroy () (at Library/PackageCache/com.arongranberg.astar@4.3.41/Generators/Base.cs:416)
Pathfinding.AstarData.ClearGraphsInternal () (at Library/PackageCache/com.arongranberg.astar@4.3.41/Core/AstarData.cs:342)
Pathfinding.AstarData.OnDestroy () (at Library/PackageCache/com.arongranberg.astar@4.3.41/Core/AstarData.cs:361)
AstarPath.OnDestroy () (at Library/PackageCache/com.arongranberg.astar@4.3.41/Core/AstarPath.cs:1436)
Should I be worried? What can I do?
Sorry for the long post!
Greetings
Denzi