Sorting by Variables Help
Hello everybody. I have a script here that sorts the array of "elements" (gameobjects) by which is furtherest from the "player" gameobject. I wanted to know how to convert this script so that I can sort (ascending) common variables in the scripts which are attached to the gameobjects. Each gameobject have a script attached to it called "Bullets" with a variable called "amount".
I want to sort the gameobjects based on the variables instead of by distance.
Can anyone help me?
The Script:
pragma strict
var elements: GameObject[]; var player : GameObject;
function Update () { System.Array.Sort(elements, Comparison); }
function Comparison(x : GameObject, y : GameObject) : int { var xDistance = (x.transform.position - player.transform.position).magnitude; var yDistance = (y.transform.position - player.transform.position).magnitude; return xDistance - yDistance; }