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
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
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
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
correction the position thing did work, but it is still not finding the gameobject
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.