Is there a way to detect if a button was pressed?

I’m making a script where you can change the ship by pressing a button, the problem is that since there is one condition, the last ship is enabled after one press. How would I detect if the button was pressed down so that the script works as intended?

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

public class NextScript : MonoBehaviour
{

public GameObject shipOne;
public GameObject shipTwo;
public GameObject shipThree;
public GameObject shipFour;
public GameObject shipFive;
public GameObject shipOneBody;
public GameObject shipTwoBody;
public GameObject shipThreeBody;
public GameObject shipFourBody;
public GameObject shipFiveBody;

public void NextShip()
{
    if(shipOne.activeInHierarchy == true)
    {
        shipOne.SetActive(false);
        shipOneBody.SetActive(false);
        shipTwo.SetActive(true);
        shipTwoBody.SetActive(true);
    }

    if(shipTwo.activeInHierarchy == true)
    {
        shipTwo.SetActive(false);
        shipTwoBody.SetActive(false);
        shipThree.SetActive(true);
        shipThreeBody.SetActive(true);
    }

    if(shipThree.activeInHierarchy == true)
    {
        shipThree.SetActive(false);
        shipThreeBody.SetActive(false);
        shipFour.SetActive(true);
        shipFourBody.SetActive(true);
    }

    if(shipFour.activeInHierarchy == true)
    {
        shipFour.SetActive(false);
        shipFourBody.SetActive(false);
        shipFive.SetActive(true);
        shipFiveBody.SetActive(true);
    }
}

}

Replace your current script by this one:

using System;
using UnityEngine;

[Serializable]
public struct ShipParts
{
    [SerializeField]
    private GameObject[] parts;
    public void SetActive(bool active)
    {
        for (int i = 0; i < parts.Length; ++i)
            parts*.SetActive(active);*

}
}

public class ShipSelector : MonoBehaviour
{

public ShipParts[] Ships;
private int selectedShipIndex;

public void SelectShip(int shipIndex)
{
if (shipIndex < 0 || shipIndex >= Ships.Length)
throw new ArgumentOutOfRangeException( “shipIndex” );

Ships[selectedShipIndex].SetActive(false);
selectedShipIndex = shipIndex;
Ships[selectedShipIndex].SetActive(false);
}

public void SelectNextShip()
{
int shipIndex = selectedShipIndex + 1;
if (shipIndex >= Ships.Length)
shipIndex = 0;
SelectShip(shipIndex);
}

public void SelectPreviousShip()
{
int shipIndex = selectedShipIndex - 1;
if (shipIndex < 0)
shipIndex = Ships.Length - 1;
SelectShip(shipIndex);
}
}
Then, in your inspector, set the Ships > Size to 5 and, for each element, set the size to 2 and drag & drop the parts of your ships.
Then, specify the SelectNextShip or SelectpreviousShip function in your button’s onClick event