Disabling canvases in an array on another script

Hi, so I have a script for general UI and a more specific script for one of my menus. The general ui script holds the different menus in an array and iterates through the array to disable them all/enable the active one. I have a function that toggles them but when I try to call that function from another script it says that the object reference is not set to an instance of an object.

I think I know why that is happening, please correct me if I’m wrong but I’m guessing the instances in the array are only accessible from that script?

How would I change this so that the function would work outside of the script.

UI Script:

using UnityEngine;
using System.Collections;

public class UIScripts : MonoBehaviour
{

    public bool menuOpen = false;

    public GameObject[] uiList;
    int currentMenu = 0;


    // Use this for initialization
    void Start ()
    {
       
    }
   
    // Update is called once per frame
    void Update ()
    {
        OpenCloseMenu();

        if (Input.GetKeyDown("escape"))
        {
            if(menuOpen == true)
            {
                menuOpen = false;
            }
            else
            {
                menuOpen = true;
            }
        }
    }

    //Checks if menu is open, if yes, opens current selected menu
    public void OpenCloseMenu ()
    {
        if (menuOpen == false)
        {
            for (int i = 0; i < uiList.Length; i++)
            {
                uiList[i].SetActive(false);
            }
        }
        else
        {
            for (int i = 0; i < uiList.Length; i++)
            {
                if (i != currentMenu)
                {
                    uiList[i].SetActive(false);
                }
                else
                {
                    uiList[i].SetActive(true);
                }

            }
        }
    }

    public void CloseMenus()
    {
        for (int i = 0; i < uiList.Length; i++)
        {
            menuOpen = false;
            uiList[i].SetActive(false);
        }
    }
}

BuildingUI Script

using UnityEngine;
using System.Collections;

public class BuildingUI : MonoBehaviour {

    [SerializeField]
    GameObject player;

    [SerializeField]
    GameObject uiManager;

    ObjectPlacement objectPlacementScript;

    UIScripts uiScript;

    // Use this for initialization
    void Start ()
    {
        objectPlacementScript = player.GetComponent<ObjectPlacement>();
        uiScript = transform.parent.GetComponent<UIScripts>();
    }
   
    // Update is called once per frame
    void Update ()
    {

   
    }

    public void SetBuilding(int button)
    {
        objectPlacementScript.SetObject(button);
        uiScript.CloseMenus();
    }
}

The SetBuilding function is called when the button is pressed. It is only the uiScript.CloseMenus which is causing the problem.

Cheers in advance

nope, public array, public function; you can access both the array from anywhere in the scene, and call the function from anywhere in the scene.

are you referring to line 33 in the second script? if that is throwing a null ref exception then there is an issue with setting up the reference on line 20.

Daft one I am, thanks for pointing that out! It was literally the transform.parent. part messing it up!

Also you may want to put your openclosemenu() call within your if statement so it’s not constantly running that method and doing the same thing over and over. You really only need them turned on or off when the button is pressed.