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!!
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!!
for( int i = 0; i < cubes.Length; i++){
if( cubes[i].name == "Cube 3"){
elementNumber = i;
}
}
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"