0

if i subscribe to event in any of the following ways i get this error in runtime

option 1

for (int i = 0; i < QuickBarBuild.QuickButtons.Length; i++)
{
    LocalInputManager.Singleton.m_QuickBlockInput[i].DownEvent += () => { QuickBarBuild.QuickButtons[i].OnPointerClick(null); };
}

option 2

for (int i = 0; i < QuickBarBuild.QuickButtons.Length; i++)
{
    LocalInputManager.Singleton.m_QuickBlockInput[i].DownEvent += () => { BuildingController.BlockPrefab = QuickBarBuild.QuickButtons[i].BlockPrefab; };
}

option 3

for (int i = 0; i < QuickBarBuild.QuickButtons.Length; i++)
{
    LocalInputManager.Singleton.m_QuickBlockInput[i].DownEvent += delegate { QuickBarBuild.QuickButtons[i].OnPointerClick(null); };
}

Lengths of arrays are equal and all elements in them are initialized and the following code works fine, and it's variants like in the options above work too

LocalInputManager.Singleton.m_QuickBlockInput[0].DownEvent += () => { QuickBarBuild.QuickButtons[0].OnPointerClick(null); };
LocalInputManager.Singleton.m_QuickBlockInput[1].DownEvent += () => { QuickBarBuild.QuickButtons[1].OnPointerClick(null); };
LocalInputManager.Singleton.m_QuickBlockInput[2].DownEvent += () => { QuickBarBuild.QuickButtons[2].OnPointerClick(null); };
LocalInputManager.Singleton.m_QuickBlockInput[3].DownEvent += () => { QuickBarBuild.QuickButtons[3].OnPointerClick(null); };
LocalInputManager.Singleton.m_QuickBlockInput[4].DownEvent += () => { QuickBarBuild.QuickButtons[4].OnPointerClick(null); };
LocalInputManager.Singleton.m_QuickBlockInput[5].DownEvent += () => { QuickBarBuild.QuickButtons[5].OnPointerClick(null); };
LocalInputManager.Singleton.m_QuickBlockInput[6].DownEvent += () => { QuickBarBuild.QuickButtons[6].OnPointerClick(null); };
LocalInputManager.Singleton.m_QuickBlockInput[7].DownEvent += () => { QuickBarBuild.QuickButtons[7].OnPointerClick(null); };
LocalInputManager.Singleton.m_QuickBlockInput[8].DownEvent += () => { QuickBarBuild.QuickButtons[8].OnPointerClick(null); };
LocalInputManager.Singleton.m_QuickBlockInput[9].DownEvent += () => { QuickBarBuild.QuickButtons[9].OnPointerClick(null); };

actual error

IndexOutOfRangeException: Index was outside the bounds of the array.
PlayerSyncedInput+<>c__DisplayClass100_0.<OnSetLocalPlayer>b__9 () (at Assets/Scripts/PlayerSyncedInput.cs:183)
LocalInputManager+InputElement.set_State (System.Boolean value) (at Assets/Scripts/LocalInputManager.cs:71)
PlayerSyncedInput.ManageInput () (at Assets/Scripts/PlayerSyncedInput.cs:451)
PlayerSyncedInput.Update () (at Assets/Scripts/PlayerSyncedInput.cs:291)

why is that happening?

ThaiCat
  • 1
  • 1
  • Your event handler, `() => { QuickBarBuild.QuickButtons[i].OnPointerClick(null); }` is a closure that captures the Loop control variable `i`. By the time the event handler is invoked, `i` has been incremented to past the last index of the array. There are a number of ways to adjust your code, I would probably write `foreach (var (input, button) in LocalInputManager.Singleton.m_QuickBlockInput.Zip(QuickBarBuild.QuickButtons)) { input.DownEvent += () => button.OnPointerClick(null); }` – Aluan Haddad Feb 25 '22 at 04:55

0 Answers0