Comparing values of an array


After alot of searching i just can’t seem to find a solution to this problem i’m having.

I have an array which holds the distances of the playercar and the 3 ai cars to the finishline.
How would i go and compare these values, and making a guitext show the players position.

I’m still learning hard, so maybe i’m in way over my head, but it just seems that i’m so close now, but it’s still missing a small vital part :frowning:

Any help is really appreciated, and here is what i have so far:

var cars : Transform[];
var finish : Transform;
var maxDistance = 0; 
var minDistance = 10000;
var distance : float[];

function Update () {

for(i=0; i<4; i++){

      distance _= (finish.position - cars*.position).magnitude;*_

if(distance < minDistance)
minDistance = distance*;*
if(distance > maxDistance)
maxDistance = distance*;*

function OnGUI(){

for(i=0; i<4; i++){

GUILayout.Label("Distance " + i + ": " + distance*);*

First up, put the line

import System.Collections.Generic;

at the top, to make sure that you have all the classes that you'll need for this.

Now, make a variable called

var sortedCars : SortedList.<Float, Transform>;

This is what you will use to keep track of the cars' positions.

In your Update function, use this:

sortedCars = new SortedList.<Float, Transform>();
for(var carTrans : Transform in cars)
    var carDistance : float = (finish.position - carTrans.position).magnitude;
    sortedCars.Add(float, carTrans);

Now, having done that, you can use this list in your OnGUI function to draw a list of cars, by their distance from the finish line!

var i : int = 0;
for(var curPosition : KeyValuePair.<Float, Transform> in sortedCars)
    GUILayout.Label(i + ": " + + ", " + curPosition.Key + " metres from finish");

I think that should do it, although I admit that I'm not too familiar with generics in UnityScript. In fact, I wasn't even aware that they existed until quite recently! If there are any errors, just tell me and I'll try to fix up my answer.