How can I shift index numbers in a decremental manner?
I have a flexible list that can have objects added to or removed from at any given time. Right now, I'm trying to update the index numbers whenever any object is removed. To put it specifically, I want the index numbers of all items with a greater index value than the removed item to decrement by 1. After that, I want the maximum Index value to update and reflect the changes to the inventory. However, I'm having an extremely difficult time getting it to work because I have the mechanism set up in a way that controls the inventory via a gamepad instead of the mouse. The issue is that all the attempts I have pulled would either make the succeeding item of the selected one disappear with it or remove all succeeding items. Often times, it would take two taps on the gamepad to select the next item. Can someone help me with this? Thanks! Relevant Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class InventoryManager : MonoBehaviour
{
public int index;
public int maxIndex;
[SerializeField] RectTransform contentTransform;
[SerializeField] bool buttonDown;
[Header("Inventory Info")]
public PlayerInventory playerInventory;
[SerializeField] private GameObject blankInventorySlot;
[SerializeField] private GameObject inventoryPanel;
[SerializeField] private TextMeshProUGUI descriptionText;
public bool isPressRight, isPressLeft, isPressConfirm;
public int HorizontalMovement;
PlayerControls controls;
bool useButtonDown;
public InventoryItem currentItem;
public bool inventoryActive;
public Animator animInven;
public void Awake()
{
controls = new PlayerControls();
controls.Gameplay.UseItem.performed += ctx => onPressConfirm();
controls.Gameplay.UseItem.canceled += ctx => onReleaseConfirm();
controls.Gameplay.BringUpInventory.performed += ctx => inventoryActive = true;
controls.Gameplay.CloseInventory.performed += ctx => inventoryActive = false;
}
public void SetTextAndSelectionStatus(string description, bool usable, InventoryItem newItem)
{
currentItem = newItem;
descriptionText.text = description;
}
void MakeInventorySlots()
{
if (playerInventory)
{
int inventIndex = 0;
foreach (InventoryItem item in playerInventory.myInventory)
{
if(blankInventorySlot != null && playerInventory.myInventory[inventIndex].numberHeld > 0)
{
InventorySlot newSlot = Instantiate(blankInventorySlot, transform.position, transform.rotation).GetComponent<InventorySlot>();
newSlot.transform.SetParent(inventoryPanel.transform);
newSlot.Setup(playerInventory.myInventory[inventIndex], this, inventIndex);
inventIndex++;
}
else
{
playerInventory.myInventory.Remove(playerInventory.myInventory[inventIndex]);
}
}
inventIndex = maxIndex;
}
}
// Start is called before the first frame update
void Start()
{
MakeInventorySlots();
isPressRight = isPressLeft = false;
useButtonDown = false;
}
public void OnPressRight()
{
isPressRight = true;
}
public void onReleaseRight()
{
isPressRight = false;
}
public void onPressLeft()
{
isPressLeft = true;
}
public void onReleaseLeft()
{
isPressLeft = false;
}
public void onPressConfirm()
{
if (!useButtonDown)
{
if (currentItem)
{
currentItem.Use();
isPressConfirm = true;
ClearInventorySlots();
MakeInventorySlots();
}
}
}
public void onReleaseConfirm()
{
isPressConfirm = false;
}
void ClearInventorySlots()
{
for(int i = 0; i < inventoryPanel.transform.childCount; i++)
{
Destroy(inventoryPanel.transform.GetChild(i).gameObject);
}
}
// Update is called once per frame
void Update()
{
maxIndex = playerInventory.myInventory.Count - 1;
animInven.SetBool("active", inventoryActive);
if (Input.GetAxis("DPad") != 0 && !isPressConfirm)
{
if (!buttonDown)
{
if (Input.GetAxis("DPad") > 0)
{
if (index < maxIndex)
{
index++;
if (index > 3 && index < maxIndex - 2)
{
contentTransform.offsetMin -= new Vector2(58, 0);
}
}
else
{
index = 0;
contentTransform.offsetMin = new Vector2(-400, -58);
}
}
else if (Input.GetAxis("DPad") < 0)
{
if (index > 0)
{
index--;
if (index < maxIndex - 3 && index > 2)
{
contentTransform.offsetMin -= new Vector2(-58, 0);
}
}
else
{
index = maxIndex;
contentTransform.offsetMin = new Vector2(-400 - ((maxIndex - 6) * 58), -58);
}
}
buttonDown = true;
}
}
else
{
buttonDown = false;
}
}
private void OnEnable()
{
controls.Gameplay.Enable();
}
private void OnDisable()
{
controls.Gameplay.Disable();
}
}
For the sake of space, I will leave out other relevant codes unless requested. Thanks again!