Select slot with same key

Hello, I have an inventory in my game and when the player plays with controller, it switches to the controller input.
Well with the keyboard, it is simple. the player can select which slot he want to hold by pressing the number pad 1 till 4 (I have only 4 slots)
But with the ps4 controller, I stuck with the problem since I want the slot switch to the next slot with the R1 button and to switch to the previous one, you have to press the L1.
If you press R1 while you hold the 4th slot, it needs to switch back to slot 1.

I really need help with this one.
Can someone help me out with this one?

This is what I got so far…

public List<SlotsSelector> slotsSelectors;
        public EquipArea equipArea;
        public Inventory inventory;

        public delegate void OnSelectSlot(int indexOfSlot);
        public event OnSelectSlot onSelectSlot;

        void Start()
        {
            inventory = GetComponentInParent<Inventory>();

            for (int i = 0; i < slotsSelectors.Count; i++)
            {
                onSelectSlot += slotsSelectors*.Select;*

}

onSelectSlot?.Invoke(0);
}

void Update()
{
if (!inventory || !equipArea || inventory.lockInventoryInput) return;

for (int i = 0; i < slotsSelectors.Count; i++)
{
if (Input.GetButtonDown(“PS4_R1”) && slotsSelectors_.indexOfSlot >= 0 && slotsSelectors*.indexOfSlot <= 2 && (inventory && !inventory.IsLocked() && !inventory.isOpen && inventory.canEquip))
{_
_slotsSelectors.indexOfSlot ++;
}*_

if (Input.GetButtonDown(“PS4_L1”) && (inventory && !inventory.IsLocked() && !inventory.isOpen && inventory.canEquip))
{
if (slotsSelectors_.indexOfSlot < equipArea.equipSlots.Count && slotsSelectors*.indexOfSlot >= 0)
{_

_equipArea.SetEquipSlot(slotsSelectors.indexOfSlot);_
_onSelectSlot?.Invoke(slotsSelectors.indexOfSlot);
}
}*_

if (slotsSelectors.equipDisplay != null && slotsSelectors_.indexOfSlot < equipArea.equipSlots.Count && slotsSelectors*.indexOfSlot >= 0)
{
if (equipArea.equipSlots[slotsSelectors.indexOfSlot].item != slotsSelectors.equipDisplay.item)
{
slotsSelectors.equipDisplay.AddItem(equipArea.equipSlots[slotsSelectors.indexOfSlot].item);
}
else if (equipArea.equipSlots[slotsSelectors.indexOfSlot].item == null && slotsSelectors.equipDisplay.hasItem)
{_

_slotsSelectors.equipDisplay.RemoveItem();
}
}
}
}*_

[System.Serializable]
public class SlotsSelector
{
public int indexOfSlot;
public EquipmentDisplay equipDisplay;
public bool selected;
public void Select(int indexOfSlot)
{
if (this.indexOfSlot != indexOfSlot && selected)
{
equipDisplay.onDeselect.Invoke();
selected = false;
}
else if (this.indexOfSlot == indexOfSlot && !selected)
{
equipDisplay.onSelect.Invoke();
selected = true;
}
}
}
I’m really dizzy right now, just trying to figure this out.

@ibouchallikht I removed a bunch of your code, to minimize it and create a sort of starting point for further development for you.
This code takes into consideration how to wrap around the index of your slots. Tinker with it and look at the changes in the inspector. From now on you can built upon it, adding further logic step by step.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Game : MonoBehaviour
{
	public List<SlotsSelector> slotsSelectors = new List<SlotsSelector>();
	public delegate void OnSelectSlot(int indexOfSlot);
	public event OnSelectSlot onSelectSlot;
	
	public int index = 0;
	
	void Start()
	{
		for(int i = 0; i < slotsSelectors.Count; i++)
		{
			onSelectSlot += slotsSelectors*.Select;*
  •  }*
    
  •  onSelectSlot?.Invoke(index);*
    
  • }*

  • void Update()*

  • {*

  •  if (Input.GetButtonDown("PS4_R1"))*
    
  •  {*
    
  •  	index = (++index)%slotsSelectors.Count;* 
    
  •  	onSelectSlot?.Invoke(index);*
    
  •  }*
    
  •  else if (Input.GetButtonDown("PS4_L1"))*
    
  •  {*
    
  •  	index = (--index + slotsSelectors.Count)%slotsSelectors.Count;* 
    
  •  	onSelectSlot?.Invoke(index);*
    
  •  }*
    
  • }*
    }

[System.Serializable]
public class SlotsSelector
{

  • public int indexOfSlot;*

  • public EquipmentDisplay equipDisplay;*

  • public bool selected;*

  • public void Select(int indexOfSlot)*

  • {*

  •  selected = (this.indexOfSlot == indexOfSlot);*
    
  •  if (!selected)*
    
  •  {*
    
  •  	equipDisplay.onDeselect.Invoke();*
    
  •  }*
    
  •  else*
    
  •  {*
    
  •  	equipDisplay.onSelect.Invoke();*
    
  •  }*
    
  • }*
    }

Showing your code would make this a bit easier, but I’m assuming you are using an array or list to represent your inventory currently. You could make an int to hold the current spot in your inventory you have currently selected like so:

int selectedItem = 0;

void Update()
{
    if(Input.GetButtonDown(psRt))
    {    
        selectedItem++;

        if(selectedItem > 3) selectedItem = 0;    
    }
    currentItem = inventory[selectedItem];
}