Need Help with Creating Menus [SOLVED]

Hello all,

In my game I have a hierarchy set like this:

MenuObjects (Empty Game Object)
—MenuCanvas (Canvas)
------TitlePanel
------EndGamePanel
------HUDPanel

How the heck do I access each of these through C# scripts to become active/inactive? Then how do I also highlight the buttons on each panel using a controller?

I was somewhat able to get this to work when I had each panel as a seperate canvas instead. Yet, that quickly became confusing in code and didn’t really work well with the EventSystem.

Here is some example code of what I was trying to do. Yet, this clearly isn’t working well…at all :frowning:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class MenuControls : MonoBehaviour
{
    GameObject titleMenu, endGameMenu, startButton, restartButton;

    void Start ()
    {
        titleMenu = GameObject.Find("TitleMenu");
        endGameMenu = GameObject.Find("EndGameMenu");
        startButton = GameObject.Find("StartButton");
        restartButton = GameObject.Find("RestartButton");
    }
   
    void Update ()
    {
        if (titleMenu.activeSelf)
        {
            Debug.Log("in title menu");
            EventSystem.SetSelectedGameObject(startButton);
        }

        if (endGameMenu.activeSelf)
        {
            Debug.Log("endgame menu");
            EventSystem.SetSelectedGameObject(restartButton);
        }
    }
}

Thanks!

here is what I would start with to get the button to switch the UI. inside unity you just add an OnClick() event and point them to the public functions that I created.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
 
public class MenuControls : MonoBehaviour
{
    GameObject titleMenu, endGameMenu, startButton, restartButton;
 
    void Start ()
    {
        titleMenu = GameObject.Find("TitleMenu");
        endGameMenu = GameObject.Find("EndGameMenu");
        startButton = GameObject.Find("StartButton");
        restartButton = GameObject.Find("RestartButton");
    }
   
    void Update ()
    {
        if (titleMenu.activeSelf)
        {
            Debug.Log("in title menu");
            EventSystem.SetSelectedGameObject(startButton);
            //do some code to change the button color...
        }
 
        if (endGameMenu.activeSelf)
        {
            Debug.Log("endgame menu");
            EventSystem.SetSelectedGameObject(restartButton);
            //do some code to change the button color...
        }
    }

    public void titleMenuBtnClicked()
    {
        setAllMenuFalse();
        titleMenu.setActive(true);
    }

    public void endGameMenuBtnClicked()
    {
        setAllMenuFalse();
        endGameMenu.setActive(true);
    }

    public void startButtonBtnClicked()
    {
        setAllMenuFalse();
        startButton.setActive(true);
    }

    public void restartButtonBtnClicked()
    {
        setAllMenuFalse();
        restartButton.setActive(true);
    }

    private void setAllMenuFalse()
    {
        titleMenu.setActive(false);
        endGameMenu.setActive(false);
        startButton.setActive(false);
        restartButton.setActive(false);
    }

}
1 Like

On these lines I’m getting an error:

EventSystem.SetSelectedGameObject(startButton);

“An object reference is required…”

Are panels not considered GameObjects?

panels are gameObjects
I’m not sure what that eventsystem.Set.SelectedGameObject() is doing for you,
make the startButton public so you can see it in the inspector. my guess is that the find is failing.
is the panel named “StartButton” in the inspector?

public GameObject titleMenu, endGameMenu, startButton, restartButton;

1 Like

did you get that error after you used my code? or was it always there?

1 Like

That error was not a result of your code. It was my code prior to adding your bits. No worries.

So, using a modified version of your code (I have the panels going active/inactive in another script) everything seems to work in terms of screens turning on and off when I need them to. However, I’m still not sure how to get the buttons on the screens to activate.

titleMenu and endGameMenu each have a button (startbutton and restartbutton). When each screen is active I want the eventsystem to automatically highlight those buttons so I can then activate them with my game pad by pressing X.

That is what that line of code was for. But it’s clearly not working :frowning:

Ok figured it out. I was calling Eventmanager incorrectly.

EventSystem.current.SetSelectedGameObject(startButton);

Thanks for the help dude.

cool, good to hear you got it working

Just a few days ago there was an asset released that does panel switching very well.
It’s called Woo Panels. There is a forum thread as well. Check it out! Cheers!