- Home /
Question by
uurr · Feb 21, 2015 at 07:24 PM ·
arrayontriggerenterarray of gameobjectsuntiy
why if loops doesnot work?
anybody knows why loops doesnot work? altough the car enter the trigger,"if loop " doesnot recognize array
var test : GameObject[];
function OnTriggerEnter(collision : Collider) { if (collision.gameObject.name==test[1]) { //do something
}
Comment
Answer by karljj1 · Feb 21, 2015 at 07:25 PM
Thats not a loop, your only accessing the 2nd item (index 1). If you want to loop through all of them then do something like this:
foreach( var item in test )
if( item.name == collision.gameObject.name )
// Do something
A standard for loop would be like this:
for( int i = 0; i < test.Length; ++i )
if( test[i].name == collision.gameObject.name )
// Do something