How do you access the Scripts, Functions, and Bools within an array of GameObjects?
I'm trying to develop a system that allows me to switch out menus when the player presses a button. I already have a HideMenu and ShowMenu function in a class on each menu GameObject, each attached to a button. Whenever these occur, they switch a isShown boolean. However, I want it so that when the player presses another button, it performs HideMenu for any menus for which isShown is active. I can't just use active and not active because I'm tweening them, not disabling them. Each Sliding Menu is its own GameObject, with its own MenuBehavior script.
My idea was this:
Declare an array of GameObjects called slideMenus in a separate MenuManager script.
In start, assign all GameObjects with the tag 'SlideMenu' into this array.
In a SwitchMenu function (called by the buttons), perform a foreach loop for each item in the array.
In the loop, if isShown is true, activate the function HideMenu.
Code:
public class MenuManager : MonoBehaviour {
private GameObject[] slideMenus;
public void Start()
{
slideMenus = GameObject.FindGameObjectsWithTag("SlideMenu");
}
public void SwitchMenu ()
{
foreach (GameObject menu in slideMenus)
{
if (menu.GetComponent<MenuBehaviour>().isShown)
menu.GetComponent<MenuBehaviour>().HideMenu();
}
}
}
The code doesn't present any errors, but in practice I get
"NullReferenceException: Object reference not set to an instance of an object MenuManager.SwitchMenu () (at Assets/Scripts/MenuManager.cs:58) UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:153) UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:630) UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:765) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269) UnityEngine.EventSystems.EventSystem:Update()"
as an error.
Any help would be appreciated.
EDIT: Alternatively, I could do the reverse and make ShowMenu just hide any other menus that are active, but that presents the same kind of problem with accessing the MenuBehaviour of other menus.
First i'd add some Debug.Log outputs to see what's going on. $$anonymous$$ost like the crash occurs because menu.GetComponent() returns null at some point.
Question is: Does it do that for all found menus or for one? $$anonymous$$aybe you have forgotten to add the $$anonymous$$enuBehaviour in one menu? Or you have a tagged an object "Slide$$anonymous$$enu" although it's not a menu.
Just adding Debug.log( menu.name ) inside the loop might already give a hint.
Well now I just feel silly: One of them WAS missing the script. Everything works as expected now, thank you.