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;
AstarController.Scan();
//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;
GetNewPath();
}
//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);
tankCompass.LookAt(path.vectorPath[currentWaypoint]);
tankBody.rotation = Quaternion.Lerp(tankBody.rotation, tankCompass.rotation, Time.deltaTime*turnSpeed);
//transform.LookAt(path.vectorPath[currentWaypoint]);
//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