I ended up not using scroll view and instead went for a refurbished version of my original design. UI is working, but the disabling/enabling of items is not working properly. Basically, player receives any item that they can interact with will be placed as a child gameobject under "Tool Handler." So all the items you see on the hotbar are inside the Tool Handler gameobject. The script I have is supposed to control what gets deactivated and activated when scrolling. Here is the script used ```cs
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.InputSystem;
public class SwitchItems : MonoBehaviour
{
//This script disables items that are not being held by the player, and enables the ones that are. (both UI and functonality)
[SerializeField] List<GameObject> hotbarImageObjects = new List<GameObject>(5); //These are the images on the hotbar
[SerializeField] List<GameObject> currentItems = new List<GameObject>(1); //any gameobject under "Tool Handler" which can be found as a child in player.
[SerializeField] RectTransform highlight; //the outline on the hotbar that goes over the selected item
private int hotbarSelectIndex = 0;
public int HotbarSelectIndex
{
get
{
return hotbarSelectIndex;
}
set
{
//This code prevents the index used in hotbarImageObjects to go out of bounds
if (value > hotbarImageObjects.Count-1) value = 0;
else if (value < 0) value = hotbarImageObjects.Count-1;
hotbarSelectIndex = value;
}
}
void OnMouseScroll(InputValue value)
{
if (value.Get<float>() == 0) return;
if (value.Get<float>() > 0)
HotbarSelectIndex--;
else if (value.Get<float>() < 0)
HotbarSelectIndex++;
//outlines the selected item
highlight.position = hotbarImageObjects[hotbarSelectIndex].GetComponent<RectTransform>().position;
SelectedItem();
}
void SelectedItem()
{
//The functionality behind switching items
foreach (var item in currentItems)
{
item.SetActive(false);
}
currentItems[hotbarSelectIndex].SetActive(true);
}
public void NewItem(GameObject item)
{
//called by other scripts when a new item wants to be added to the player and its hotbar
currentItems.Add(item);
}
}
The problem lies within the SelectedItem method, Specifically **line 52** with the `hotbarSelectIndex` integer. the integer can have a possible value between 0, the start of the hotbar, and 5, the end of the hotbar. If the values inputted go over, it will wrap, EX: if you put a value that goes over 5 in the integer, the integer will set to 0 and vice versa. Problem is, there is always five slots in the hotbar and no less, but the player doesn't constantly have five items, in fact they start out with only one item. Because of this, the `hotbarSelectIndex` sometimes has a index value too large for the `currentItems` list and causes an out of bounds error. **I need to find a solution to where not having five items still allows me to properly deactivate/activate items correctly.** I tried thinking of possible solutions by myself but can't even think one possible idea, I can send more information and pictures if needed.