Next and Previous Button

I’m trying to figure out how to script these “PREVIOUS” and “NEXT” buttons.

I was going to use an array with gameobjects and an int to get the current int and current array int, so that I can toggle the gameobjects visibility on and off, but all I’m completley lost. I need someone pointing me into the right direction.

This is what I got so far:

  public GameObject[] mailPrefab;

    int currentIndex = 0;

    public int CurrentIndex
    {
        get
        {
            return currentIndex;
        }
        set
        {
            if (value < 0)
                currentIndex = mailPrefab.Length - 1;
            else if (value > mailPrefab.Length - 1)
                currentIndex = 0;
            else
                currentIndex = value;

            if (mailPrefab[currentIndex] != null)
            {
                GameObject aktivesObj = mailPrefab[currentIndex];
                aktivesObj.SetActive(true);
            }
            else
            {
             
            }
        }
    }

    public void Next(int direction)
    {
        if (direction == 0)
            CurrentIndex--;
        else
            CurrentIndex++;
    }

I’ve seen so many of these kind of menu navigation, so I guess it can’t be that hard, may it be that my basic idea is just wrong? How would / do you do this kind of things?

should work actually… however you don’t set any game object inactive so you probably have all the game objects active . that might be the reason why you think it doesn’t work.

PS: the direction parameter is not intuitive. I would pass -1 for backwards and +1 for forward if I would try to use that function. Then you also wouldn’t need an if statement (just “CurrentIndex += direction”). You can also make it better readable by passing “bool forwards” instead of the int.

Your code assumes that all objects are not active to begin with. Then as soon as one becomes active, as @Hosnkobf said, there’s nothing to make them not active again. I’d add something like this to your MonoBehavior:

    //sets everything except the first gameObject to inactive
    void Awake()
    {
        if (mailPrefab.Length > 0) //you might not need this check, but it will catch any weird edge cases
        {
            for (int i = 1; i < mailPrefab.Length; ++i)
            {
                mailPrefab[i].SetActive(false);
            }
        }
    }
    public int CurrentIndex
    {
        get
        {
            return currentIndex;
        }
        set
        {
            if (mailPrefab[currentIndex] != null)
            {
                //set the current active object to inactive, before replacing it
                GameObject aktivesObj = mailPrefab[currentIndex];
                aktivesObj.SetActive(false)
            }

            if (value < 0)
                currentIndex = mailPrefab.Length - 1;
            else if (value > mailPrefab.Length - 1)
                currentIndex = 0;
            else
                currentIndex = value;
            if (mailPrefab[currentIndex] != null)
            {
                GameObject aktivesObj = mailPrefab[currentIndex];
                aktivesObj.SetActive(true);
            }
        }
    }

Thank you Hosnkobf and TheWanderingBen!!

I tried to set the GameObject inactive, but the only result was me making a mess.
But thanks to you two it finally works, you’re great!:slight_smile:

1 Like