using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ButtonManager : MonoBehaviour
{
#region Variables
[Header("Scroll Settings")]
[SerializeField] private Scrollbar scrollbarHorizontal;
[Header("Buttons Reference")]
[SerializeField] private GameObject bottomPanel;
private List<Button> bottomPanelButtonsList = new();
private List<float> scrollbarValuesList = new();
private float listCount;
private const float scrollbarValueForShop = 0f;
private const float scrollbarValueForLeaderboard = 0.25f;
private const float scrollbarValueForHome = 0.5f;
private const float scrollbarValueForFriends = 0.75f;
private const float scrollbarValueForSettings = 1;
#endregion
#region In-Built Functions
private void Awake()
{
scrollbarHorizontal.value = scrollbarValueForHome; //To always start from Home
PopulateButtonList();
PopulateScrollbarValueList();
//AddListenerToButtons();
}
private void Start()
{
//AddListenerToButtons();
OnButtonClick();
}
#endregion
#region Custom Functions
private void PopulateButtonList()
{
if (bottomPanelButtonsList != null)
{
bottomPanelButtonsList.AddRange(bottomPanel.GetComponentsInChildren<Button>());
Debug.Log("Button List Populated");
}
else
{
bottomPanelButtonsList = new();
}
}
private void PopulateScrollbarValueList()
{
if (scrollbarValuesList != null)
{
scrollbarValuesList.Add(scrollbarValueForShop);
scrollbarValuesList.Add(scrollbarValueForLeaderboard);
scrollbarValuesList.Add(scrollbarValueForHome);
scrollbarValuesList.Add(scrollbarValueForFriends);
scrollbarValuesList.Add(scrollbarValueForSettings);
Debug.Log("Float List Populated");
}
else
{
scrollbarValuesList = new();
}
}
private void OnButtonClick()
{
if (bottomPanelButtonsList.Count == scrollbarValuesList.Count)
{
listCount = bottomPanelButtonsList.Count;
Debug.Log($"{listCount}"); //5
for (int i = 0; i < listCount; i++)
{
//bottomPanelButtonsList[0].onClick.AddListener(() => scrollbarHorizontal.value = scrollbarValuesList[0]);
//bottomPanelButtonsList[1].onClick.AddListener(() => scrollbarHorizontal.value = scrollbarValuesList[1]);
//bottomPanelButtonsList[2].onClick.AddListener(() => scrollbarHorizontal.value = scrollbarValuesList[2]);
//bottomPanelButtonsList[3].onClick.AddListener(() => scrollbarHorizontal.value = scrollbarValuesList[3]);
//bottomPanelButtonsList[4].onClick.AddListener(() => scrollbarHorizontal.value = scrollbarValuesList[4]);
bottomPanelButtonsList[i].onClick.AddListener(() => scrollbarHorizontal.value = scrollbarValuesList[i]);
}
}
}
#endregion
}
Error Message:
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
System.Collections.Generic.List1[T].get_Item (System.Int32 index) (at <9aad1b3a47484d63ba2b3985692d80e9>:0) ButtonManager+<>c__DisplayClass14_0.<OnButtonClick>b__0 () (at Assets/Scripts/ButtonManager.cs:88) UnityEngine.Events.InvokableCall.Invoke () (at <86acb61e0d2b4b36bc20af11093be9a5>:0) UnityEngine.Events.UnityEvent.Invoke () (at <86acb61e0d2b4b36bc20af11093be9a5>: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 target, 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:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)