- Home /
Waypoint NullReferenceException: Object reference not set to an instance of an object error please help.
ok i keep getting this error but i have no idea how to fix it. Please help. :)
i basically have 3 script. One is the waypoint script, second is a player script and enemy script. Player script have a function call FindClosestEnemy to find the closest waypoint around the player. Then it store the name in to the static var closestpos:GameObject. After this i want in my WAYPOINT script var closest = playerscript.closestpos. And caculate by if/else function weather the waypoint.transform.position.z is smaller or bigger than the closestpos.transform.position. if yes do sth...if no do sth...etc.
i able to make it work in game scene, but it just keep poping up this error in my console. Can someone help me please. /bow
MY PLAYER SCRIPT
static var closestpos: GameObject;
function Update()
{
FindClosestEnemy();
}
function FindClosestEnemy () : GameObject {
// Find all game objects with tag Enemy
var waypoint : GameObject[];
waypoint = GameObject.FindGameObjectsWithTag("waypoint");
var closest : GameObject;
var distance = Mathf.Infinity;
var position = transform.position;
for (var closestwaypoint : GameObject in waypoint) {
var diff = (closestwaypoint.transform.position - position);
var curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closest = closestwaypoint;
distance = curDistance;
closestpos = closest;
}
} return closest;
}
MY WAYPOINT SCRIPT
var waypoint : Transform[];
private var currentWaypoint: int;
var player : Transform;
var saveTime = 0;
private var closest: GameObject;
function Update()
{
Debug.Log(playerscript.closestpos.transform.position);
closest = playerscript.closestpos;
var position = transform.position;
if(transform.position.z < closest.transform.position.z)
//NullReferenceException: Object reference not set to an instance of an object waypoint.Update () (at Assets/scripts/waypoint.js:15)
{ currentWaypoint=1;
} else {
if(transform.position.z > closest.transform.position.z)
{ currentWaypoint=0;
} else {
if(transform.position.x > closest.transform.position.x)
{
currentWaypoint=2;
} else {
if(transform.position.x < closest.transform.position.x)
{
currentWaypoint=3; } } } } }
function OnTriggerEnter (enemy : Collider) {
if (enemy.gameObject.tag == "enemy")
{
var rotation = waypoint[currentWaypoint].rotation;
var position = waypoint[currentWaypoint].position;
enemy.transform.LookAt(position);
}
}
..humm to make question simpler.. why this line have error?
" if(transform.position.z < closest.transform.position.z) "
the var called closest = playerscript.closest and the playerscript.closest is a static var : GameObject
Double-click the error to find the line. Exactly what is it looking for that is NULL? $$anonymous$$aybe you don't any objects tagged as "waypoint"? $$anonymous$$aybe it can't find playerScript? ... . When you get a error like this, it skips the rest of the script, but runs again next frame.
when i double kick it. the error happen in this line
if(transform.position.z < closest.transform.position.z)
ooh i fix it... i need make a yield waitforseconds to delay the if/else function