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!