How to get Element number from array

Ok, so I have an Array with a lot of cubes. But how would I get the Element number of Cube 3 in a script?
I made the array like this public GameObject[] cubes;

Thx!!
6457735--723868--upload_2020-10-26_12-54-13.png

for( int i = 0; i < cubes.Length; i++){

if( cubes[i].name == "Cube 3"){
elementNumber = i;
}
}
1 Like

Of course if you have a reference to your cube3 gameobject there’s no need to compare the names but just compare the references.

public GameObject[] cubes;

GameObject cube; // the cube reference you want to find:
int index = -1;
for( int i = 0; i < cubes.Length; i++)
{
    if(cubes[i] == cube)
    {
        index = i;
        break;
    }
}
// here you should have the index of "cube" inside the "cubes" array.
// Though if the cube you're looking for does not exist in the array you get "-1"
1 Like