Error: Coroutine couldn't be started because the the game object is inactive!

I’m using the AIFollow script on all my enemy GameObjects, which I have in a pool which I enable/disable as needed.

Since I upgraded to Unity 4 and the latest A* Pathfinding build, I’m getting this error:

Coroutine couldn't be started because the the game object 'Bat(Clone)' is inactive! UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) AIFollow:OnPathComplete(Path) (at Assets/Plugins/AstarPathfindingProject/ExampleScenes/ExampleScripts/AIFollow.cs:82) Seeker:OnPathComplete(Path, Boolean, Boolean) (at Assets/Plugins/AstarPathfindingProject/Core/AI/Seeker.cs:296) Seeker:OnPathComplete(Path) (at Assets/Plugins/AstarPathfindingProject/Core/AI/Seeker.cs:257) Pathfinding.Path:ReturnPath() (at Assets/Plugins/AstarPathfindingProject/Core/Path.cs:583) AstarPath:ReturnPaths(Boolean) (at Assets/Plugins/AstarPathfindingProject/Core/AstarPath.cs:1939) c__Iterator9:MoveNext() (at Assets/Plugins/AstarPathfindingProject/Core/AstarPath.cs:1891)

It links me to this section of the code:

`	/** Called when a path has completed it's calculation */
	public void OnPathComplete (Path p) {
		
		/*if (Time.time-lastPathSearch >= repathRate) {
			Repath ();
		} else {*/
			StartCoroutine (WaitToRepath ());    // <~~ CAUSE OF ERROR
		//}
		
		//If the path didn't succeed, don't proceed
		if (p.error) {
			return;
		}
		
		//Get the calculated path as a Vector3 array
		path = p.vectorPath.ToArray();
		
		//Find the segment in the path which is closest to the AI
		//If a closer segment hasn't been found in '6' iterations, break because it is unlikely to find any closer ones then
		float minDist = Mathf.Infinity;
		int notCloserHits = 0;
		
		for (int i=0;i<path.Length-1;i++) {
			float dist = Mathfx.DistancePointSegmentStrict (path[i],path[i+1],tr.position);
			if (dist < minDist) {
				notCloserHits = 0;
				minDist = dist;
				pathIndex = i+1;
			} else if (notCloserHits > 6) {
				break;
			}
		}
	}`

Any suggestions on how to avoid the error? Although it seems relatively harmless…

Ah, I see.
What happens is that an AI issues a path request, then the AI is deactivated, a few frames later when the path request is completed, the AI is inactive and thus, the coroutine cannot be started. You can solve this by, at the start of the OnPathComplete function add something like:

if (!this.active) return;
I think the AIPath script can handle starting to search for paths again when becoming active again, but you might want to check that to make sure.

Thanks Aron, that solved the problem. I believe enemy pooling is quite popular for optimization (rather than Instantiating / Destroying) in Unity. Hopefully this helps others. :slight_smile:

Ah, unfortunately you were right. When re-enabling the enemies, they’re not going anywhere :frowning:

Any suggestions?

Hm.
Try setting “canSearchAgain = true;” before returning. If that is not set, it will think it is waiting for a path request to complete.

I think I figured it out. I needed to call Repath() upon re-activating the enemy.