- Home /
Why am I getting the 2nd and 3rd closest objects wrong
I wrote a script to spawn random gas stations and find the closest 3 in order from closest to furthest from the truck. The script finds and returns the closest gas station spot on every time. but it only returns the second closest on sometimes. and the third closest one usually is wrong. Any idea what could be causing this issue?
#pragma strict
// vars for game objects
var gasStation : GameObject;
var numberOfGasStationsToSpawn : int = 10;
private var myPosition : Vector2;
// arrays
var gasStationArray : GameObject[];
var gasStationPosition : Vector2[];
// counters
private var locations : int = 0;
private var numberOfStationsChecked : int = 0;
// vars to find the closest station
private var closest : String;
private var secondClosest : String;
private var thirdClosest : String;
private var checking : float = 0;
private var closestDistanceFromTruck : float = 50;
private var secondClosestDistanceFromTruck : float = 50;
private var thirdClosestDistanceFromTruck : float = 50;
// vars to keep an execution order
private var allSpawned : boolean = false;
private var allPositionsFound : boolean = false;
// for the other scripts... and this one
static var numberOfGasStations : int = 0;
// for testing purposes
var test : float = 0;
function Start ()
{
gasStationArray = new GameObject[numberOfGasStationsToSpawn];
gasStationPosition = new Vector2[numberOfGasStationsToSpawn];
}
function Update ()
{
myPosition.Set(gameObject.transform.position.x, gameObject.transform.position.y);
if(allSpawned == false)
{
CreateGasStations(); // This function will be deleted in the final project.
}
if(allSpawned == true)
{
GetGasStationsLocations();
}
if(allPositionsFound == true)
{
FindClosestGasStation();
}
}
function CreateGasStations()
{
// This is just for testing and setting up. Will be deleted later;
if(numberOfGasStations < numberOfGasStationsToSpawn)
{
var newStation : GameObject = Instantiate (gasStation, Vector3(Random.Range(43.0,57.0), Random.Range(39.0,50.0), 0), Quaternion.identity);
gasStationArray[numberOfGasStations] = newStation;
numberOfGasStations++;
}
else
{
allSpawned = true;
}
}
function GetGasStationsLocations()
{
if(locations < numberOfGasStations)
{
gasStationPosition[locations].Set(gasStationArray[locations].transform.position.x,gasStationArray[locations].transform.position.y);
locations++;
}
else
{
allPositionsFound = true;
}
}
function FindClosestGasStation()
{
if(numberOfStationsChecked < numberOfGasStations)
{
checking = Vector2.Distance(myPosition, gasStationPosition[numberOfStationsChecked]);
if(Mathf.Abs(checking) < closestDistanceFromTruck)
{
closestDistanceFromTruck = checking;
closest = gasStationArray[numberOfStationsChecked].name;
}
else if(checking < secondClosestDistanceFromTruck)
{
secondClosestDistanceFromTruck = checking;
secondClosest = gasStationArray[numberOfStationsChecked].name;
}
else if(checking < thirdClosestDistanceFromTruck)
{
thirdClosestDistanceFromTruck = checking;
thirdClosest = gasStationArray[numberOfStationsChecked].name;
}
numberOfStationsChecked++;
}
else
{
if(closest != null)
{
print("The closest is " + closest + ". It is " + closestDistanceFromTruck + " miles away.");
print("The second closest is " + secondClosest + ". It is " + secondClosestDistanceFromTruck + " miles away.");
print("The third closest is " + thirdClosest + ". It is " + thirdClosestDistanceFromTruck + " miles away.");
}
}
}
Your whole code structure is quite odd... You're using Update() and instance variables as a virtual for loop which just seems precarious. As for the distance checks you are only overwriting the first, second, or third gas station. The first will always be correct as this is checked first and is absolute. If the second closest changes the old second closest is removed, when it should be set to the third closest. You are also initializing the distances to check for first/second/third to the same, see$$anonymous$$gly random, value.
I suggest you take a step back and rethink the design of your code here...
$$anonymous$$y code structures are always odd lol. And now that you say it the problem seems so simple. When I was originally running through the code in my head I had knew to watch out for that and then I guess I forgot lol. Thanks man.
Your answer
![](https://koobas.hobune.stream/wayback/20220613121509im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Spawn along local axis. 3 Answers
Finding Distances With Vector3 1 Answer
Get distance between moving object and a static one? 1 Answer