Friday, June 10, 2022
HomeGame Developmentunity - Argument out of Vary Exception on click on of button...

unity – Argument out of Vary Exception on click on of button that calls a operate that does not use an index


At the moment writing a script that builds a HUD for testing some features in a mission. It mechanically builds buttons, one in all which works, whereas the opposite provides the error indicated within the title and error message under.

The operate the button is looking works when known as by a button manually positioned within the scene, indicating that the problem is not with the operate itself however the way in which the button is being constructed.

The script compiles profitable and are rendered accurately within the edtior. Clicking the radio button (which builds the button that’s giving the error) works accurately, constructing the button and working the debug logs.

The error happens when clicking on the “subsequent music” button, which is generated by clicking on the radio button.

Right here is the error:

ArgumentOutOfRangeException: Index was out of vary. Should be non-negative and fewer than the scale of the gathering.
Parameter identify: index
System.Collections.Generic.Record`1[T].get_Item (System.Int32 index) (at <6073cf49ed704e958b8a66d540dea948>:0)
TestHUD+<>c__DisplayClass12_0.<BuildActions>b__0 () (at Property/Scripts/TestHUD.cs:86)
UnityEngine.Occasions.InvokableCall.Invoke () (at <f1212ad1dec44ce7b7147976b91869c3>:0)
UnityEngine.Occasions.UnityEvent.Invoke () (at <f1212ad1dec44ce7b7147976b91869c3>:0)
UnityEngine.UI.Button.Press () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:70)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:114)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:57)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject goal, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:272)
UnityEngine.EventSystems.EventSystem:Replace() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)

Listed here are the Debug Logs

Size of buttonActions is 1.

Operate button created

Right here is the script because it at present exists:

utilizing System;
utilizing System.Collections.Generic;
utilizing TMPro;
utilizing UnityEngine;
utilizing UnityEngine.UI;

public class TestHUD : MonoBehaviour
{
    GameObject testHUD;

    public Record<TestHUDPanel> testHUDPanels = new();

    public GameObject sectionPanel;
    public GameObject functionPanel;

    public GameObject sectionPrefab;
    public GameObject functionPrefab;

    //Color32 activeColor = new Color32(255, 201, 191, 255);
    //Color32 inactiveColor = new Color32(191, 255, 241, 255);

    SoundManager soundManager;

    public class TestHUDPanel
    {
        public string identify;
        public GameObject linkedObject;
        public GameObject panelObject;
        public Record<Motion> buttonActions = new();
        public Record<string> buttonLabels = new();

        public TestHUDPanel(string panelName)
        {
            identify = panelName;
        }

        public void AddButton(string buttonName, Motion buttonFunc)
        {
            buttonActions.Add(buttonFunc);
            buttonLabels.Add(buttonName);
        }
    }

    non-public void Awake()
    {
        testHUD = gameObject;
        soundManager = GameObject.Discover("SoundManager").GetComponent<SoundManager>();
    }

    non-public void Begin()
    {
        BuildPanelList();
        BuildPanels();
    }

    public void BuildPanelList()
    {
        testHUDPanels.Add(new TestHUDPanel("Radio"));
        testHUDPanels[0].AddButton("Subsequent Tune", soundManager.PlayNextSong);
        testHUDPanels.Add(new TestHUDPanel("Typing"));
        testHUDPanels.Add(new TestHUDPanel("Audio"));
    }


    public void BuildPanels()
    {
        foreach (TestHUDPanel panel in testHUDPanels)
        {
            GameObject panelObject = Instantiate(sectionPrefab);
            panelObject.rework.SetParent(sectionPanel.rework);
            Button button = panelObject.GetComponent<Button>();
            panelObject.GetComponentInChildren<TextMeshProUGUI>().textual content = panel.identify;
            button.onClick.AddListener(() => BuildActions(panel));
        }
    }

    public void BuildActions(TestHUDPanel panel)
    {
        for (int i = 0; i < panel.buttonActions.Rely; i++)
        {
            GameObject panelObject = Instantiate(functionPrefab);
            panelObject.rework.SetParent(functionPanel.rework);
            Button button = panelObject.GetComponent<Button>();
            panelObject.GetComponentInChildren<TextMeshProUGUI>().textual content = panel.buttonLabels[i];
            Debug.Log(string.Format("Size of buttonActions is {0}, ", panel.buttonActions.Rely));
            >>> THIS IS LINE 86 <<< : button.onClick.AddListener(() => panel.buttonActions[i]());
            Debug.Log("Operate button created");
        }
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments