0

I have a code segment that looks like this:

53    Debug.Log("Palette Count: " + palette.Count);
54    Debug.Log("Selection: " + selection);
55    palette[selection].isOn = false;

For reference:

palette is a List<Toggle>

selection is an int

The code works perfectly, except when I add the following to the Start function:

20    for (int i = 0; i < palette.Count; i++) {
21        palette[i].onValueChanged.AddListener(delegate { setSelect(i); });
22    }

For reference:

194    private void setSelect(int index) {
195        Toggle toggle = palette[index];
196        if (toggle.isOn) {
197            selection = index;
198        }
199    }

Once these segments are included, I receive the two Debug.Log() outputs and a runtime error:

Palette Count: 9
UnityEngine.Debug:Log(Object)
UIManagerW:Update() (at Assets/Scripts/World/UIManagerW.cs:53)
Selection: 0
UnityEngine.Debug:Log(Object)
UIManagerW:Update() (at Assets/Scripts/World/UIManagerW.cs:54)
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <437ba245d8404784b9fbab9b439ac908>:0)
System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <437ba245d8404784b9fbab9b439ac908>:0)
System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at <437ba245d8404784b9fbab9b439ac908>:0)
UIManagerW.setSelect (System.Int32 index) (at Assets/Scripts/World/UIManagerW.cs:195)
UIManagerW+<>c__DisplayClass11_0.<Start>b__0 (System.Boolean <p0>) (at Assets/Scripts/World/UIManagerW.cs:21)
UnityEngine.Events.InvokableCall`1[T1].Invoke (T1 args0) (at <d815b7efac424eeb8e053965cccb1f98>:0)
UnityEngine.Events.UnityEvent`1[T0].Invoke (T0 arg0) (at <d815b7efac424eeb8e053965cccb1f98>:0)
UnityEngine.UI.Toggle.Set (System.Boolean value, System.Boolean sendCallback) (at C:/Program Files/2019.3.6f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Toggle.cs:280)
UnityEngine.UI.Toggle.set_isOn (System.Boolean value) (at C:/Program Files/2019.3.6f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Toggle.cs:243)
UIManagerW.Update () (at Assets/Scripts/World/UIManagerW.cs:55)

I'm not really sure how this is possible. I can restore the functionality by commenting out the lines within setSelect, so it would seem that Line 195 is the culprit. I just haven't a clue what to go off of here.

Thanks in advance for any help you can provide!

Anslean
  • 19
  • 1
  • 5
  • Does the error occur when you run the for loop, or during runtime? – KingOfArrows Apr 13 '20 at 05:30
  • It occurs when during runtime when I execute a user input (scroll wheel) that changes the value of `selection`. – Anslean Apr 13 '20 at 05:35
  • Is it possible that you have altered the contents of the palette list after your example code has been run? You're passing the index to a listener (which I believe wont get run until later), meaning that if the palette list has had entries removed the index range has shortened and your original index is now out of sync/incorrect. – KingOfArrows Apr 13 '20 at 05:40
  • `for (int i = 0; i < palette.Count;` change '<' to '<=' – mcalex Apr 13 '20 at 05:43
  • @KingOfArrows I don't change the palette during runtime at all. I'm gonna take a look at madreflection 's linked post and be back shortly. – Anslean Apr 13 '20 at 05:45
  • @madreflection's assessment was true. Does this question get closed for being a duplicate? – Anslean Apr 13 '20 at 05:49

0 Answers0