Reputation: 25
So i currently have an array that takes and stores a certain value from the 5 heroes a player picks. This value is the heroes initiative which i will use to see what heroes go first and such. So for example hero one gets saved to the h1initiative variable that is saved to array[0] and hero two gets saved to the h2initiative variable that is saved to array[1] and so on. But i have a sorting function at the bottom of the script that i believe to be working though I'm not sure. But I'm not completely sure how use the sorting function to allow me to sort the variables in the code from highest to lowest and then activate the codes in order. So for example Archer is saved to h1initiative/array[0] with a value of 3 and berserker is saved to h2initiative/array[1] with a value of 5. I would want to activate the berserkers code first and then the archers after the berserker is done since the archer has the next highest value. I know the code that will allow me to activate a scripts i just need to know how to code for the highest code to do this, and then the next one down and so on. Heres my code:
using UnityEngine;
using System.Collections;
public class InitiativeSorter : MonoBehaviour {
GameObject hOne;
GameObject hTwo;
GameObject hThree;
GameObject hFour;
GameObject hFive;
GameObject MainCamera;
public int h1Initiative = 0;
public int h2Initiative = 0;
public int h3Initiative = 0;
public int h4Initiative = 0;
public int h5Initiative = 0;
public int[] initiativeArray = new int[5];
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
initiativeArray[0] = h1Initiative;
initiativeArray[1] = h2Initiative;
initiativeArray[2] = h3Initiative;
initiativeArray[3] = h4Initiative;
initiativeArray[4] = h5Initiative;
MainCamera = GameObject.Find("Main Camera");
HeroArraySaver heroArraySaver = MainCamera.GetComponent<HeroArraySaver>();
hOne = heroArraySaver.heroOne;
hTwo = heroArraySaver.heroTwo;
hThree = heroArraySaver.heroThree;
hFour = heroArraySaver.heroFour;
hFive = heroArraySaver.heroFive;
Initiative initiative = hOne.GetComponent<Initiative>();
h1Initiative = initiative.heroInitiative;
Initiative initiative1 = hTwo.GetComponent<Initiative>();
h2Initiative = initiative1.heroInitiative;
Initiative initiative2 = hThree.GetComponent<Initiative>();
h3Initiative = initiative2.heroInitiative;
Initiative initiative3 = hFour.GetComponent<Initiative>();
h4Initiative = initiative3.heroInitiative;
Initiative initiative4 = hFive.GetComponent<Initiative>();
h5Initiative = initiative4.heroInitiative;
System.Array.Sort<int>(initiativeArray);
}
}
Upvotes: 0
Views: 105
Reputation: 8171
If you want access OrderByDescending
method, you have to add using of linq to your program by:
using System.Linq;
Then OrderByDescending
is accesible:
var sortedArray = initiativeArray.OrderByDescending(item => item);
And you could access highest value by:
var highestValue = sortedArray.First()
Also if you want to access nth element you could use following code:
var 1st = sortedArray.ElementAt(0);
var 2nd = sortedArray.ElementAt(1);
var 3rd = sortedArray.ElementAt(2);
...
Upvotes: 1
Reputation: 5305
var sortedArray = initiativeArray.OrderByDescending(item => item);
Upvotes: 3