- Home /
Question by
SimplyMabu · Dec 09, 2016 at 06:19 AM ·
c#arrayvariablesgameobjects
comparing variables of gameobjects in an array
Hello, I have an array with two game objects in it and I want to compare their int variables "ATK" and see which one is bigger (and then destroy the game object that has the smaller). The problem is that the specific game objects that make up the array change throughout the game so I want to call the ATK variable just using the index of the array and I don't know how to access a game object's variables in an array like that
I don't know if I managed to ask my question clearly but any help would be appreciated!
Comment
Best Answer
Answer by DoubleIsLoveDoubleIsLife · Dec 09, 2016 at 08:43 AM
float highestAtk = 0;
GameObject toDestroy;
for(int i = 0; i < whateveryourarrayis.Length; i++) {
if(whateveryourarrayis[i].GetComponent<whatcomponenttheattackvariableisin>().atk > highestAtk) {
toDestroy = whateveryourarrayis[i];
highestAtk = whateveryourarrayis[i].GetComponent<whatcomponenttheattackvariableisin>.atk;
}
}
Destroy(toDestroy);