Find Nearest Object?

Hey guys, have a script here that substitutes my old one, because no one apparently knows what Arrays are here.

// the tag to search for (set this value in the inspector)

var searchTag = "Questions";



// the frequency with which to re-scane for new nearest target in seconds 

// (set in inspector)

var scanFrequency = 0.1;



// the current target

private var target : Transform;





function Start() {

    // set up repeating scan for new targets:

    InvokeRepeating("ScanForTarget", 0, scanFrequency );

}









function ScanForTarget() {

    // this should be called less often, because it could be an expensive

    // process if there are lots of objects to check against

    target = GetNearestTaggedObject();



}



function GetNearestTaggedObject() : Transform {

    // and finally the actual process for finding the nearest object:



    var nearestDistanceSqr = Mathf.Infinity;

    var taggedGameObjects = GameObject.FindGameObjectsWithTag(searchTag); 

    var nearestObj : Transform = null;



    // loop through each tagged object, remembering nearest one found

    for (var obj : GameObject in taggedGameObjects) {



        var objectPos = obj.transform.position;

        var distanceSqr = (objectPos - transform.position).sqrMagnitude;



        if (distanceSqr < nearestDistanceSqr) {

            nearestObj = obj.transform;

            nearestDistanceSqr = distanceSqr;

        }

    }



    return nearestObj;

}



function OnTriggerEnter(other : Collider){

	if(other.tag == "QuestionCube"){

		transform.LookAt(target);

}

}

Once again, no errors, no results.

Why isn’t this working?

seems to work for me…(?)

tried with,
// find line

target = GetNearestTaggedObject();

//add line after it to print name of the closest object…

print (target.name);

Strange. I have no idea why, but it just isn’t doing anything. If I set it to print when It finds the target, it doesn’t print anything.

What, no one?

Ah! I fixed it. Turns out I didn’t have it looping with function Update()

silly me!

I don´t find any problem. works fine.

// the tag to search for (set this value in the inspector)
var searchTag = "Player";
 
// the frequency with which to re-scane for new nearest target in seconds 
 
// (set in inspector)
 
var scanFrequency = 0.1;
 
// the current target
 
private var target : Transform;
function Start()
{    // set up repeating scan for new targets:
    InvokeRepeating("ScanForTarget", 0, scanFrequency);
}
 
function ScanForTarget()
{ 
    // this should be called less often, because it could be an expensive
    // process if there are lots of objects to check against
    target = GetNearestTaggedObject();
    print (target.name);
}
 
function GetNearestTaggedObject() : Transform
{ 
    // and finally the actual process for finding the nearest object:
 
    var nearestDistanceSqr = Mathf.Infinity;
 
    var taggedGameObjects = GameObject.FindGameObjectsWithTag(searchTag); 
 
    var nearestObj : Transform = null;
 
    // loop through each tagged object, remembering nearest one found
 
    for (var obj : GameObject in taggedGameObjects)
    {
 
        var objectPos = obj.transform.position;
 
        var distanceSqr = (objectPos - transform.position).sqrMagnitude;
 
 
        if (distanceSqr < nearestDistanceSqr)
        {
 
            nearestObj = obj.transform;
 
            nearestDistanceSqr = distanceSqr; 
        } 
    }
 
    return nearestObj;
 
}
function OnTriggerEnter(other : Collider)
{
 
    if(other.tag == "Player")
    { 
        transform.LookAt(target); 
 }
 
}