Null Exception in for loop

Hi there, i am a beginner with A* and i made a scene where enemies are spawned, using A* they find their way to the end. But when i put an obstacle in front of them they should recalculate the path, this is my code:

Debug.Log(“Start”);
for(var theEnemy : GameObject in GameObject.FindGameObjectsWithTag(“Enemy_Ground”))
{
Debug.Log(theEnemy);
theEnemy.GetComponent(Enemy_Tank).GetNewPath();
Debug.Log(“Path for this is Complete”);
}
Debug.Log(“End”);

i try to search for every object with the tab “Enemy_Ground” and call GetNewPath(); for each of them.

function GetNewPath()
{
seeker.StartPath(transform.position, targetPositon, OnPathComplete);
}

But i receive a null exception at the “theEnemy.GetComponent(Enemy_Tank).GetNewPath();” line. With Debug.Log(theEnemy); i see that theEnemy is not a null, it’s never null. sometimes i get the null at the 1st 2nd or nth iteration. why i receive the null exception at some of them, the same prefab is spawn.
One more thing, in console the “Path Completed” from A* is after my “Debug.Log(“Path for this is Complete”);”. It should be between my two logs, not after them. I hope you understood my problem.

Hi

Apparently you do not have an Enemy_Tank component on all your objects which are tagged “Enemy_Ground”.

The path requests are not calculated immediately, that’s why the log message is not printed directly.

Yes i have, because like i said, for some of them works and it recalculates the path, i can see that the green line changes.