Question How do I get a reference to the next element in my list?

Girl_Number_4

New member
Joined
Nov 27, 2022
Messages
1
Programming Experience
Beginner
Hey people I'm trying to make a sort of inventory system for my game when the player presses e they basically add a sprite to a list. My issue is I want to use the Q button to cycle through the items in the list but I have no idea how to go about getting the next element in the list here is my code so far.

C#:
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public class HarvestPlant : MonoBehaviour
{
    public bool isInHarvestRange;
    public Image image;
    public Sprite harvestedSprite;
    public List<Sprite> sprites = new List<Sprite>();
    public Sprite selectedSprite;
    public void HarvestPlantFunction()
    {
        sprites.Add(harvestedSprite);
    }
    private void Update()
    {
        image.sprite = selectedSprite;
        if (Input.GetKey(KeyCode.Q))
        {         
            Debug.Log("Q pressed");
            selectedSprite = sprites[1];
            // so basically the idea is that everytime you press q selected Sprite will go up in the elements by 1 value but if it's at max value return to the lowest value
        }
      
    }
}
 
Last edited by a moderator:
You need to know the index of the current item so that you can increment that to get the item at the next index. You have two main options for that.

The first option is to maintain a separate variable for the index of the current item. That might be -1 by default, to indicate no item, or 0 to indicate the first item. When you want the next item, you simply increment that current item index variable and then get the item at the current index.

The second option is to not maintain a separate variable but get the index on demand. You can pass the current item to the IndexOf method of the list to get its index, then increment that value, then get the item at the new index.
 
Second option is a bit slow though, compared to the first, because list will visit every item in turn checking to see if the item passed it IndexOf is at that index. As lists get longer that can have a noticeable performance impact

Just like you have a Sprite selectedSprite you could instead have a int selectedSpriteIndex and use it like you have used [1]

You can have your Q keypress routine increase it and if you increase it like this:

selectedSpriteIndex = (selectedSpriteIndex + 1) % sprites.Count

The mod operator will make it wrap round to 0 again when it goes higher than the count of the items in the list. Mod produces a result between 0 and one less than the divisor, so eg something%10 produces a result between 0 and 9. This is handy for lists and indexes which are 0 based indexing because a number mod by the length/count of the array/list results in an index that is within the array/list somewhere. If there were 10 items in your list and your index is on 9, plus one is 10, mod by 10 is 0, so it wraps around to the start
 
Last edited:
A third option is to not use the original list directly but to create a Queue from that list, then Dequeue items one at a time. When the Queue is empty, you can create a new one and start again. This option has the drawback of not changing the queue if/when the original list changes, which may not be an issue. The separate index variable is going to be the best performing option though, as suggested above.
 
Back
Top Bottom