Collider and Renderer disabling of gameObjects is seemingly random

Hello, I have a simple level, and I want to set [.enabled = false] on all enemies’ collider and renderer components, but it seems to work at random.

I have a script in which I set up all colliders and renderers in two arrays:

7891102--1004470--EnemyColRend Script.png

and one where I tell the game to deactivate all enemy colliders + renderers as soon as the level is completed:

I use ‘‘i + 4’’ because one 3d object is imported from Blender and is made of several smaller 3d objects.

The setup in Unity:

When I test it all, the game will sometimes deactivate that collider but not this renderer, sometimes it’ll deactivate both on one object and it’s the big surprise for others. Why is it inconsistent ? And how can I fix this ? I would really appreciate your help !

The GetComponentsInChildren<T>() documentation does guarantee order.

Does your i vs i+4 logic rely on a particular order or correlation?

No, it doesn’t rely on any special order or correlation. As long as they’re all disabled, the goal would be met.

Got it ! It was all about array indexing logic !

If I write “i + 5” that means the disabling starts at the i+5th element, instead of the ith element. Therefore, all elements starting from the i+5th position and after are disabled, but all elements before are not, because I told the array to start in a further position. So instead of using one iterator for both colliders and renderers, I used one for each component: