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