Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Collider and Renderer disabling of gameObjects is seemingly random

Discussion in 'Scripting' started by BloomyFractal, Feb 12, 2022.

  1. BloomyFractal

    BloomyFractal

    Joined:
    May 8, 2021
    Posts:
    16
    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:

    EnemyColRend Script.png

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

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

    The setup in Unity:
    Unity Editor.png

    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 !
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    The
    GetComponentsInChildren<T>()
    documentation does guarantee order.

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

    BloomyFractal

    Joined:
    May 8, 2021
    Posts:
    16
    No, it doesn't rely on any special order or correlation. As long as they're all disabled, the goal would be met.
     
    Last edited: Feb 13, 2022
  4. BloomyFractal

    BloomyFractal

    Joined:
    May 8, 2021
    Posts:
    16
    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:

    LevelEnds Corrected function.png