NullReferenceException: Object reference not set to an instance of an object (41760)

the script is supposed to find 1 gameobject from a group of tagged objects in scene, then have the object this script is attached to go towards it. right now its not relaying the current target to the turn and move towards part of the script. a bit rusty on my scripting could use some quick help. i know its something simple too…irks me.

PLEASE HELP!!!

public var speed : float = 5;
var followrange : float;
var stoprange : float;
static var canmove : boolean = true;
var closestSurvivor : GameObject;
var stopped : boolean = false; 
function start ()
{}

function GetClosestSurvivor(tag:String) : GameObject
{
var objectsWithTag = GameObject.FindGameObjectsWithTag("Survivor");
for (var obj : GameObject in objectsWithTag)
{
    if(!closestSurvivor)
    {
       closestSurvivor = obj;
    }
    //compares distances
    if(Vector3.Distance(transform.position, obj.transform.position) <= Vector3.Distance(transform.position, closestSurvivor.transform.position))
    {
       closestSurvivor = obj;
    }
                
}
return closestSurvivor;
}

function Update()
{
 
	if(Vector3.Distance(closestSurvivor.position, transform.position) <= followrange && stopped == false)
	{
	canmove = false;
	transform.LookAt(GameObject.closestSurvivor);
	transform.Translate(Vector3(0,0,1) * speed * Time.deltaTime);
	}
	
	
	if(Vector3.Distance(closestSurvivor.position, transform.position) <= stoprange)
	{
	canmove = false;
	transform.LookAt(GameObject.closestSurvivor);
	stopped = true;
	}
	
	
	
	if(Vector3.Distance(closestSurvivor.position, transform.position) > stoprange)
	{
	stopped = false;
	}
	
	
	
	if(Vector3.Distance(closestSurvivor.position, transform.position) > followrange)
	{
	canmove = true;
	}


}

thank you

When the subject line error is thrown, what line is it thrown at?

Could you clean up your thread? you have 7 answers now, but only 1 or 2 are actual answers. Remove all those answers that should have been comments.

7 Answers

7

I got it working if you still need it, problem was the way you were looking for the closest, the function actually:

public var speed : float = 5;
var followrange : float=5;
var stoprange : float=10;
var closestSurvivor : GameObject;
var timer:float;

function Start (){
closestSurvivor = GetClosestSurvivor();    
timer =0;}

function GetClosestSurvivor () : GameObject {
    var objectsWithTag = GameObject.FindGameObjectsWithTag("Survivor"); 
    var closest : GameObject; 
    var distance = Mathf.Infinity; // Here assign a large value so that first survivor will obviously be closer
    for (var obj : GameObject in objectsWithTag)  { 
        if (Vector3.Distance(transform.position, obj.transform.position) <= distance)   { 
            closest = obj; 
            // Update the closest distance with the last closer
            distance = Vector3.Distance(transform.position, obj.transform.position); 
        } 
    } 
    Debug.Log("Closest "+closest.name);
    return closest;    
}

function Update()
{
    timer += Time.deltaTime; // **Edit** I forgot this int he first sending
    if(timer>= 5){
       //Assign to the closest
       closestSurvivor = GetClosestSurvivor();
       timer = 0;
    }

    if(Vector3.Distance(closestSurvivor.transform.position, transform.position) <= stoprange)
    {
    transform.LookAt(closestSurvivor.transform.position);
      if(Vector3.Distance(closestSurvivor.transform.position, transform.position) <= followrange)
        transform.Translate(Vector3(0,0,1) * speed * Time.deltaTime);
    }
}

It is tried and definitely working.

the error is thrown at the beginning of the update function, when calling the reference to closestSurvivor.position

it is not a boolean, i need it to work with gameobjects or transforms

Remove this

The variable “closestSurvivor” type is not “Transform”. So, you should use that variable as “closestSurvivor.transform.position”.

i might have tried that, but i’ll double-check after breakfast. thanks

Remove those.

He wasn't online for more than two years, so don't expect any response.

nope, “closestSurvivor.transform.position” didnt work

same error, UnassignedReferenceException: The variable closestSurvivor of ‘EnemyAI’ has not been assigned.
You probably need to assign the closestSurvivor variable of the EnemyAI script in the inspector.
EnemyAI.Update () (at Assets/scripts/EnemyAI.js:35)

but its not getting the info

Remove this

@Bunny83 what do you think of the solution i found or my reasoning as to why it works? (putting a collider around everything that interacts with nothing, valid or not it works beutifully)

correction the position thing did work, but it is still not finding the gameobject

Remove this

function GetClosestSurvivor () : GameObject {
var objectsWithTag = GameObject.FindGameObjectsWithTag(“Survivor”);
var closest : GameObject;
var distance = Mathf.Infinity; // Here assign a large value so that first survivor will obviously be closer
for (var obj : GameObject in objectsWithTag) {
if (Vector3.Distance(transform.position, obj.transform.position) <= distance){
closest = obj;
// Update the closest distance with the last closer
distance = Vector3.Distance(transform.position, obj.transform.position);
}
}
Debug.Log("Closest "+closest.name);
return closest;
}

If distance smaller than Vector3.Distance(), function doesn’t return a GameObject. Therefore GetClosestSurvivor() function return null. This result causes “NullReferenceException: Object reference not set to an instance of an object.” error.