Updating my AI path during runtime

Hi all, first off I apologize if this has already been answered as well as if this is the wrong forum I should be asking this question. I have been working on a tower defence game for some time now and I have come to an issue that I really can not work out on my own. basically I have a gameobject (dinosaur) that spawns and heads toward a desired location. now if I place the turret before the dino is spawned then there is no issue (dino spawns and calculates the path and avoids obstacles) however if i place it as the dino is still in game I get an issue and the dino does not recalculate. the unity issue I get is NullReferenceException: Object reference not set to an instance of an object
LevelMaster.Update () (at Assets/scripts/master/LevelMaster.js:170)

to give you a better understand of my code here is a snippet of level master when I update the pathfinding

var AstarController : AstarPath;

//update all enemy paths
for(var theEnemy : GameObject in GameObject.FindGameObjectsWithTag(“Ground Enemy”))
theEnemy.GetComponent(Enemy_GroundUnit).GetNewPath(); <this is line 170

and here is the script I have attached to my dinosaur gameobject

#pragma strict

//This line should always be present at the top of scripts which use pathfinding
import Pathfinding;

class Enemy_GroundUnit extends Enemy_Base

var tankBody : Transform;
var tankCompass : Transform;
var turnSpeed : float = 10.0;

var targetPosition : Vector3; //the destination postion
var seeker : Seeker; //the seeker component on this object, this aids in building my path
var controller : CharacterController; //the charactor controller component on this object
var path : Path; //this will hold the path to follow
var nextWaypointDistance : float = 3.0; //mininum distance required to move toward next waypoint
private var currentWaypoint : int = 0; //index of the waypoint this object is currently at

//do this right away, of course!
function Start()
	targetPosition = GameObject.FindWithTag("GroundTargetObject").transform.position;

//this function, when called, will generate a new path from this object to the "targetPosition"
function GetNewPath()
	//Debug.Log("getting new path!");
	seeker.StartPath(transform.position,targetPosition, OnPathComplete); //tell the seeker component to determine the path

//this function will be called when the seeker has finished determining the path
function OnPathComplete(newPath : Path) //the newly determined path is sent over as "newPath", type of "Path"
	if (!newPath.error) //if the new path does not have any errors...
		path = newPath; //set the path to this new one
		currentWaypoint = 0; //now that we have a new path, make sure to start at the first waypoint

//this function is called by Unity every physics "frame" (ie, many times per second, much like "function Update()")
function FixedUpdate()
	if(path == null) //no path?
		return; //...then don't do anything!
	if(currentWaypoint >= path.vectorPath.Count) //reached end of path?
		return; //do...something? We'll do nothing for now...
	//find direction to next waypoint
	var dir : Vector3 = (path.vectorPath[currentWaypoint]-transform.position).normalized;
	//find an amount, based on speed, direction, and delta time, to move
	dir *= forwardSpeed * Time.fixedDeltaTime;
	//move! :)
	controller.SimpleMove (dir);
	//rotate to face next waypoint
	//transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(path.vectorPath[currentWaypoint]),1);
	tankBody.rotation = Quaternion.Lerp(tankBody.rotation, tankCompass.rotation, Time.deltaTime*turnSpeed); 
	//Check if we are close enough to the next waypoint
	if (Vector3.Distance (transform.position,path.vectorPath[currentWaypoint]) < nextWaypointDistance) 
		currentWaypoint++; //If we are, proceed to follow the next waypoint


P.S as far as I can tell it is not finding the function GetNewPath(); but I do not see why as I am calling the script via GetComponent(Enemy_GroundUnit).GetNewPath();

I hope I have explained this well enough thank you


the unity issue I get is NullReferenceException: Object reference not set to an instance of an object
LevelMaster.Update () (at Assets/scripts/master/LevelMaster.js:170)


This is some bug in your script. I cannot tell exactly what it is because I do not have the line numbers, but try to find out why you are getting that null reference exception. Do you have an Enemy_GroundUnit component attached to the same GameObject as the LevelMaster script?

thanks for getting back to me Aron, I apologize I should have provided more visual aids to reinforce my question. to answer your reply as far as I understand it (plz correct me if im wrong) but during the game, the level master script spawns the gameobject via my assigned spawn points (example 1) > then the script attached to my gameobject (example 3) takes over > the script then runs the function GetNewPath(); once to find the first AI path. then because I have placed the code within my level master update function (example 1) every time I place a tower, the A* path rescans to see if the tower is blocking the way of any of my game objects (example 1), if it is then call the GetNewPath(); function via the GetComponent(Enemy_GroundUnit).GetNewPath(); which then recalculates the game objects AIpath.

P.S I am not sure if this would effect my outcome but within example 3 on line 51, the tower defence tutorial used (currentWaypoint >= path.vectorPath.Length. however when I did it that way I was getting errors saying Assets/scripts/enemyAI/Enemy_GroundUnit.js(51,55): BCE0019: ‘Length’ is not a member of 'System.Collections.Generic.List.UnityEngine.Vector3. so to work around this after a bit of research I used (currentWaypoint >= path.vectorPath.Count. this worked fine but I can not figure out if this is why the AIpath is not working correctly

thanks Aron

(example 1 level master code)

(example 2 level master inspector)

(example 3 Enemy_GroundUnit code)

(example 4 Enemy_GroundUnit inspector )

(example 5 A* inspector 1)

(example 6 A* inspector 2)


The .Length vs .Count thing is because it was previously an array, but I have since changed it to be a List.
Was this in a particular page in the documentation? If so, could you link it so that I could update it?

Well, even with all those images, the only thing I can tell is that one of your objects that is tagged with the tag ‘Ground Enemy’ does not have the Enemy_GroundUnit component attached. I think you should debug that loop and check what objects it is actually finding. This is nothing specific to the A* Pathfinding Project.