Storing a variable and it's name

I'm attempting to take multiple variables, put them in order of highest to lowest, then extract the highest and display it's name as part of the GUI.

To be a bit more specific:

I have four variables: hunger, pain, tiredness and boredom. Each of these have a percentage rating from 0-100 (0.1, 0.2, 0.3, etc). I put the four variables in an array, sort them and reverse them, so for example if:

hunger = 0.5 pain = 0.0 tiredness = 0.2 boredom = 0.3

then the array is:

array = 0.5, 0.3, 0.2, 0.0

Now that I have my correct ordering I want to take highest value (0.5) and display 'hunger' on the GUI. But the array no longer knows that 0.5 comes from the variable 'hunger'.

How do I go about connecting the value of 0.5 to the name of the variable so I can dispaly the variable name?

Although the answer given by Bunny83 should work, I found a much quicker and simpler way of solving my specific problem.

Unity allows you to take two arrays, then sort the second based upon the values in the first. This is limited in that you must create the arrays in matching pairs. See the example below for a better understanding:

function OnGUI() {
    var dName = ["Hungry", "Pain", "Bored", "Sleepy", "Tired"];
    var dValue = [hungerPerc, painPerc, boredomPerc, sleepinessPerc, tirednessPerc];
    System.Array.Sort(dValue, dName);

    highestDrive = dName[4];
    GUI.Label(Rect(10,100, 200,20), highestDrive);

This code creates two arrays. The values stored in them need to be written in order so that the 'sort' function will link them. In my case the creatures interaction with the world will change the numeric values in the dValue's. The GUI then displays the name of the highest value.

Here an example in C#:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CHighscoreItem
    public string name;
    public float value;
    public CHighscoreItem(string aName, float aValue)
        name = aName;
        value = aValue;

public class Highscore : MonoBehaviour,IComparer<CHighscoreItem>
    public float hunger = 0.5f;
    public float pain = 0.0f;
    public float tiredness = 0.2f;
    public float boredom = 0.3f;
    List<CHighscoreItem> myList = null;

    void Start ()
        myList = new List<CHighscoreItem>();
    void Update()
        myList.Add(new CHighscoreItem("hunger",hunger));
        myList.Add(new CHighscoreItem("pain",pain));
        myList.Add(new CHighscoreItem("tiredness",tiredness));
        myList.Add(new CHighscoreItem("boredom",boredom));

    void OnGUI()
        foreach (CHighscoreItem I in myList)
            GUILayout.Label( + " : " + I.value);

    public int Compare(CHighscoreItem I1, CHighscoreItem I2)
        return I2.value.CompareTo(I1.value);

The trick to sort a list of structs/classes is to implement a Compare function for your type. I used the generic interface IComparer<> and implemented that function in my Highscore class. That's why i can sort the list that way:


Sort() take as argument an Object that implements a compare function, so i gave it "this".

The script works, just save it as Highscore.cs and put it on any gameobject. If you need a JS version, just ask for a JS freak to adapt this one :wink:

Thanks for the continued help Bunny. I'm pretty close to getting it. I think I'm only one or two steps short.

Here's the code I've got so far:

//Chemical percentages
static var hungerPerc = 0.0;
static var painPerc = 0.0;
static var tirednessPerc = 0.0;
static var sleepinessPerc = 0.0;
static var boredomPerc = 0.0;
static var driveArray = new Array();

    function Start() {

        //create seperate drive info classes
        var hungerInfo = new driveInfo("Hunger", hungerPerc);
        var painInfo = new driveInfo("Pain", painPerc);
        var tirednessInfo = new driveInfo("Tired", tirednessPerc);
        var sleepinessInfo = new driveInfo("Sleepy", sleepinessPerc);
        var boredomInfo = new driveInfo("Bored", boredomPerc);

        //place drive info classes into an array
        driveArray = [hungerInfo, painInfo, tirednessInfo, sleepinessInfo, boredomInfo];

        //sort the array in terms of highest drive


    //Create class to contain drive information
    class driveInfo implements System.Collections.Generic.IComparer{
        function driveInfo(myDriveName:String, myDriveValue:int)
            this.driveName = myDriveName;
            this.driveValue = myDriveValue;
        var driveName: String;
        var driveValue: int;

I think part of the problem may be that I'm attempting to 'sort' the array in the start function, and not in the class, but I'm really not sure.