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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Adding/Removing components from custom Inspector

Discussion in 'Scripting' started by hashashin, Jun 8, 2017.

  1. hashashin

    hashashin

    Joined:
    Mar 31, 2014
    Posts:
    22
    Hi

    I have a custom inspector that generates sphere colliders, adding them to the same game object where the inspector is attached. In the OnInspectorGUI, in case the colliders data changed, I destroy them all, and create new ones (as sometimes the number of collider changes, etc.).

    The cool part is that it looks like it is working perfectly.

    The main problem is that Unity complains with an error message (see the full stack bellow). I understand why this happens: unity is cycling through the inspectors, drawing them, and I am changing that list, making it smaller or larger. Thus, indexes out of range occur.

    One option is to way for Unity to fix this (but, should this be fixed?)
    Another option, and that is the one I am looking into, is to understand how I should mimic this behavior, without hitting this "bug".

    Suggestions?

    basic code structure here: https://gist.github.com/ambs/8ceefcc0d24321d10c11270dfeac2944

    stack----


    IndexOutOfRangeException: Array index is out of range.

    UnityEditor.InspectorWindow.EditorHasLargeHeader (Int32 editorIndex) (at /home/builduser/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:1267)

    UnityEditor.InspectorWindow.DrawEditor (UnityEditor.Editor editor, Int32 editorIndex, Boolean rebuildOptimizedGUIBlock, System.Boolean& showImportedObjectBarNext, UnityEngine.Rect& importedObjectBarRect) (at /home/builduser/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:1074)

    UnityEditor.InspectorWindow.DrawEditors (UnityEditor.Editor[] editors) (at /home/builduser/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:1010)

    UnityEditor.InspectorWindow.OnGUI () (at /home/builduser/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:361)
    System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
     
  2. Phorsaken

    Phorsaken

    Joined:
    Dec 12, 2013
    Posts:
    27
    If it is complaining about the size changing could you switch to a list instead and get around the error?

    -Alexander
     
  3. hashashin

    hashashin

    Joined:
    Mar 31, 2014
    Posts:
    22
    The problem is that the code failing is not mine, but from Unity.
    Nevertheless, I think I know how to solve this issue. In fact, not, changing completely the approach, so, not really getting around the exception.