Why can't I deactivate and activate the next / previous button?

I try to deactivate the next button on the last page of the tutorial (4) and activate it when I am on previous pages(0-3).

I would also like to deactivate the previous button when I am on the first page of the tutorial (0) and activate it when I am on the next pages of the tutorial (1-4).

Unfortunately, I don’t know why, but I can’t do it. The buttons disappear once, appear once. In turn, once it disappears, it doesn’t appear. Can anyone improve my code and explain what I did wrong?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using UnityEngine.SceneManagement;

public class MenuPanel : MonoBehaviour
{


    public GameObject[] tutorialPage;


    [SerializeField] GameObject buttonLeft;
    [SerializeField] GameObject buttonRight;

    int currentIndex = 0;

    public int CurrentIndex
    {
       
            get
            {
                return currentIndex;
            }
            set
            {
            if (tutorialPage[currentIndex] != null)
                {
                    //set the current active object to inactive, before replacing it
                GameObject aktivesObj = tutorialPage[currentIndex];
                aktivesObj.SetActive(false);
                }

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

public void Next(int direction)
{
if (direction >= 1)
    CurrentIndex++;
   

if (currentIndex >= 4)
{
buttonRight.SetActive(false);
}
        if (currentIndex <= 3)
        {
            buttonRight.SetActive(true);
        }
}

public void Previous(int direction)
{
if (direction == 0)
    CurrentIndex--;
   

if (currentIndex ==0)
{
buttonLeft.SetActive(false);
}
        if (currentIndex >= 1)
        {
            buttonLeft.SetActive(true);
        }
}           
           
        public void OnClickTutorialPanel()
    {

        currentIndex = 0;
        buttonRight.SetActive(true);
        buttonLeft.SetActive(false);

        tutorialPage[0].SetActive(true);
        Tutorial.SetActive(true);
    }




    public void OnClickBackToSettings(){
       
        tutorialPage[currentIndex].SetActive(false);
        Tutorial.SetActive(false);


    }
}

It certainly appears correct above. Check the two nested if statements on line 36… I recommend always having braces and indenting, just to make sure your logic is correct in your mind.

Try maybe using Debug.Log() to print out when it is enabling/disabling the various panels and next/prev buttons. It seems like it should work so maybe it will reveal something more.

Solved. The problem was that I referred to the if condition not in the button where it should be, which caused a contradiction.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using UnityEngine.SceneManagement;

public class MenuPanel : MonoBehaviour
{
public GameObject[] tutorialPage;


    [SerializeField] GameObject buttonLeft;
    [SerializeField] GameObject buttonRight;

    int currentIndex = 0;

    public int CurrentIndex
    {
      
            get
            {
                return currentIndex;
            }
            set
            {
            if (tutorialPage[currentIndex] != null)
                {
                    //set the current active object to inactive, before replacing it
                GameObject aktivesObj = tutorialPage[currentIndex];
                aktivesObj.SetActive(false);
                }

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

    public void Previous(int direction)
    {
        if (direction == 0)
            CurrentIndex--;
      
        if (CurrentIndex <= 3)
        {
            buttonRight.SetActive(true);

            Debug.Log("4");
        }

        if (CurrentIndex <= 0)
        {
            buttonLeft.SetActive(false);
            Debug.Log("3");
        }
    }

  

public void Next(int direction)
{
if (direction >= 1)
    CurrentIndex++;
  
        if (CurrentIndex >= 4)
        {
            buttonRight.SetActive(false);

            Debug.Log("2");
        }

        if (CurrentIndex >= 1)
        {
            buttonLeft.SetActive(true);
            Debug.Log("3");
        }

    }


    public void OnClickTutorialPanel()
    {

        if (CurrentIndex <= 0)
        {
            buttonLeft.SetActive(false);
            buttonRight.SetActive(true);
            Debug.Log("1");

        }

        tutorialPage[0].SetActive(true);
        Tutorial.SetActive(true);
    }




    public void OnClickBackToSettings(){
        CurrentIndex = 0;

        tutorialPage[currentIndex].SetActive(false);
        Tutorial.SetActive(false);

}
    }