x


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);

}

}

more ▼

asked Apr 12 '11 at 06:31 AM

Mister Jason gravatar image

Mister Jason
2 2 2 2

..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

Apr 12 '11 at 08:36 AM Mister Jason

Double-click the error to find the line. Exactly what is it looking for that is NULL? Maybe you don't any objects tagged as "waypoint"? Maybe it can't find playerScript? ... . When you get a error like this, it skips the rest of the script, but runs again next frame.

Apr 12 '11 at 03:50 PM Owen Reynolds

when i double kick it. the error happen in this line

if(transform.position.z < closest.transform.position.z)

Apr 13 '11 at 12:49 AM Mister Jason

ooh i fix it... i need make a yield waitforseconds to delay the if/else function

Apr 13 '11 at 01:19 AM Mister Jason
(comments are locked)
10|3000 characters needed characters left

0 answers: sort newest
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2087
x396
x203
x170

asked: Apr 12 '11 at 06:31 AM

Seen: 1013 times

Last Updated: Apr 12 '11 at 06:31 AM