Help with hide previous ui panel when click on an object

Hi all

i am in desperate need of help

I have 3 cube which activate a ui panel when i click on them. I added a script to make the previous one hide when acivating a new one and it’s partially working.
My problem is when i place my cubes in the hiearchy in this order from top to bottom panel3,panel2,panel1 and click 1,2,3 in this order it’s working perfectly but
if i click 3,2,1 the previous panels are not hiding and they are overlaping. If i change the hierarchy order and place them as panel1,panel2,panel3 its doing the same thing
by clicking on 3,2,1 its working fine but 1,2,3 they are overlaping. I need help because this map will have 1200 cubes total and they need to hide whichever panel that
was open previously when clicking a new cube.

Here’s the script i’m currently using

using UnityEngine;
using UnityEngine.UI;

public class MenuVisibilityCtrl : MonoBehaviour {
    [SerializeField] bool shouldStartVisible;

    GameObject _myGameObj;
    GameObject _invisibleBG;

    void Awake ()
    {
        setupInvisibleBG ();
        _myGameObj = gameObject;
        if (!shouldStartVisible)
            hide();
    }

    void setupInvisibleBG ()
    {
        _invisibleBG = new GameObject("InvisibleBG");

        InvisibleBGCtrl tempInvisibleBGCtrl = _invisibleBG.AddComponent<InvisibleBGCtrl>();
        tempInvisibleBGCtrl.setParentCtrl (this);

        Image tempImage = _invisibleBG.AddComponent<Image>();
        tempImage.color = new Color(1f, 1f, 1f, 0f);

        RectTransform tempTransform = _invisibleBG.GetComponent<RectTransform>();
        tempTransform.anchorMin = new Vector2(0f, 0f);
        tempTransform.anchorMax = new Vector2(1f, 1f);
        tempTransform.offsetMin = new Vector2(0f, 0f);
        tempTransform.offsetMax = new Vector2(0f, 0f);
        tempTransform.SetParent(GetComponentsInParent<Transform>()[1], false);
        tempTransform.SetSiblingIndex(transform.GetSiblingIndex()); // put it right beind this panel in the hierarchy
    }

    void OnEnable ()
    {
        _invisibleBG.SetActive(true);
    }

    public void hide ()
    {
        _myGameObj.SetActive(false);
        _invisibleBG.SetActive(false);
    }
}

sorry first time using forums