Finding the closest obstacle to you when you are an obstacle.

Hi. I’m trying to think of a way to find all obstacles in my game that use the obstacle tag. The confusing part is that my player is an obstacle to other players, but there is no point in counting yourself as an obstacle, so I need to exclude myself in this script. Have any idea how? All I need to know is the closest obstacle to me other than myself.

function FindThatObstacle():GameObject{

    var gos:GameObject[];
    gos=GameObject.FindGameObjectsWithTag("Obstacle"); 
    var closest:GameObject; 
    var distance=Mathf.Infinity; 
    var position=transform.position; 
    // Iterate through them and find the closest one
    for (var go : GameObject in gos)  { 
        var diff=(go.transform.position - position);
        var curDistance=diff.sqrMagnitude; 
        if (curDistance<distance) { 
            closest=go;
            distance=curDistance; 
        } 
    } 
    return closest;    
}

// […]
var position=transform.position;
// Iterate through them and find the closest one
for (var go : GameObject in gos) {
if (go.transform != transform) {
var diff=(go.transform.position - position);
var curDistance=diff.sqrMagnitude;
if (curDistance<distance) {
closest=go;
distance=curDistance;
}
}
}

This should do the trick :wink: I’m not sure if UnityScript knows continue but it’s a quite basic keyword so it should work. I’m a C# user :wink: