Why is my ID value getting stuck here?

My inventory is just an array that takes an index, and returns the item in that slot. The player increments or decrements the desired index by rolling the mousewheel, which works fine, but I’m having trouble making the number “roll”: if the array has 6 values, when desiredSlot is 5 and the player increments it, I want it to reset back to 0, and if it’s 0 when the player decrements it, I want to set it to the array.length-1, e.g. the last entry in the array. This looks right to me:

desiredSlot = 0;
if (characterInventory.generalItems.length == 0){
	return 0;
}else if (ID > characterInventory.generalItems.length){
	desiredSlot = 0;
else if (ID < 0){
	desiredSlot = characterInventory.generalItems.length - 1; //prevents calling an index that doesn't exist
return desiredSlot;

However, this doesn’t work at all when I run it- it works when there are only two items in the inventory, but when I try it with six, it constantly flip-flops between the first and last item in the array, and never selects an intermediate index. Is there an obvious hole in my logic here?

Try Mathf.Repeat

    return (int) Mathf.Repeat(deesiredSlot, characterInventory.generalItems.length);