Error Message:
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
`
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 bottomPanelButtonsList = new();
private List scrollbarValuesList = new();
private int 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();
}
private void Start()
{
OnButtonClick();
}
#endregion
#region Custom Functions
private void PopulateButtonList()
{
if (bottomPanelButtonsList != null)
{
bottomPanelButtonsList.AddRange(bottomPanel.GetComponentsInChildren());
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 = 1; 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]); //Error Here
}
}
}
#endregion
}
`
, 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 bottomPanelButtonsList = new();
private List scrollbarValuesList = new();
private int 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();
}
private void Start()
{
OnButtonClick();
}
#endregion
#region Custom Functions
private void PopulateButtonList()
{
if (bottomPanelButtonsList != null)
{
bottomPanelButtonsList.AddRange(bottomPanel.GetComponentsInChildren());
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 = 1; 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.List`1[T].get_Item (System.Int32 index) (at <9aad1b3a47484d63ba2b3985692d80e9>:0)
ButtonManager+<>c__DisplayClass14_0.b__0 () (at Assets/Scripts/ButtonManager.cs:88)