- Home /
On which place is my value in array?
I'm making nice race placing system and what i want to do now is to check on what place (after using MyArray.sort() function) is my value from MyArray after sorting. How do i do that? Script:
var EnemyAI_1 : GameObject;
var EnemyAI_2 : GameObject;
var EnemyAI_3 : GameObject;
var EnemyAI_4 : GameObject;
var EnemyAI_5 : GameObject;
var EnemyAI_6 : GameObject;
var EnemyAI_7 : GameObject;
function Update () {
Ship1 = EnemyAI_1.GetComponent(EnemyShipAI).CheckActualPlace;
Ship2 = EnemyAI_2.GetComponent(EnemyShipAI).CheckActualPlace;
Ship3 = EnemyAI_3.GetComponent(EnemyShipAI).CheckActualPlace;
Ship4 = EnemyAI_4.GetComponent(EnemyShipAI).CheckActualPlace;
Ship5 = EnemyAI_5.GetComponent(EnemyShipAI).CheckActualPlace;
Ship6 = EnemyAI_6.GetComponent(EnemyShipAI).CheckActualPlace;
Ship7 = EnemyAI_7.GetComponent(EnemyShipAI).CheckActualPlace;
Ship8 = gameObject.GetComponent(ActualDistFromFinish).CheckActualPlace;
var Place = new Array (Ship1,Ship2,Ship3,Ship4,Ship5,Ship6,Ship7,Ship8);
Place.Sort();
print(Place);
}
It would be far easier and more flexible if you used a GameObject[] array or List rather than a bunch of individual variables, and it would be faster and better code if you use List.<GameObject>
ins$$anonymous$$d of Array.
Answer by tomekkie2 · Dec 24, 2011 at 01:05 PM
You could create a function that returns an index of a GameObject element in array.
use a builtin GameObject[] array
function indexOf(arr : GameObject[], element:GameObject) {
var l1 = arr.Length;
var ind:int = 0;
for (var i=0; i<l1; i++) {
if(arr[i]==element) {
ind = i;
break;
}
}
and then:
........
Place.Sort(); var Places: GameObject[] = Place.ToBuiltin(GameObject); var indexInArray = indexOf(Places, Ship2) print(indexInArray);
Thanks for answer but the problem here is that my element in array is not an gameObject, it is a float.
if it is float, you just put float ins$$anonymous$$d of GameObject and float[] ins$$anonymous$$d of GameObject[]
Your answer
Follow this Question
Related Questions
Sort Array Of GameoObjects useing a variable 1 Answer
Change index values in list 0 Answers
How to sort an array ? 1 Answer
Looping through array to find a specific class? 0 Answers
i need a player list help please 0 Answers