Home > Software engineering >  How to access and disable a GameObject from a different script - Unity2D
How to access and disable a GameObject from a different script - Unity2D

Time:10-05

I am currently working on a game, and while designing a Main Menu for the game, I need to be able to access different GameObjects and enable/disable them for the user to access different parts of the menu. The different parts are held under their respective parent GameObjects, for example, all the GameObjects related to the "Options Menu" will be under a single empty GameObject called "Options".

Here is the code pertaining to a script that holds the references to all the different parent objects and the methods to enable them:

public class MenuSwitch : MonoBehaviour
{
    public GameObject Main;
    public GameObject Options;
    public GameObject About;
    public GameObject Load;

    public void MainMenu()
    {
        Main.SetActive(true);
        Options.SetActive(false);
        About.SetActive(false);
        Load.SetActive(false);
    } 

    public void OptionsMenu()
    {
        Main.SetActive(false);
        Options.SetActive(true);
        About.SetActive(false);
        Load.SetActive(false);
    }

However. the problem is the navigation of the Menu has to be done by the player by typing 'commands'. For example, they would type '/options' to go to the options menu. This functionality will remain throughout the game, so I made another script to handle player commands from the in-game console. Now how do I call the methods from MenuSwitch, because making a MenuSwitch object inside my console script does not seem to work, it gives an error: "Object reference not set to an instance of an object".

Here is the script handling the commands specified in the main menu:

// Commands
string[] MainMenuCommands = { "./start", "./load", "./about", "./options", "./end" };

private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Return))
        {
            // Reset the console
            playerInput = console.text;
            console.text = "";

            // Main Menu Input
            if (CheckCommand(playerInput) == true)
            {
                RunCommand(playerInput, scene);
            }
        }
    }

void RunCommand(string _command, int _scene)
    {
        // Main Menu commands

        if (_scene == 0)
        {
            if (_command == MainMenuCommands[0]) { StartCoroutine(MM_Start()); }
            else if (_command == MainMenuCommands[1]) { MM_Load(); }
            else if (_command == MainMenuCommands[2]) { MM_About(); }
            else if (_command == MainMenuCommands[3]) { MM_Options(); }
            else if (_command == MainMenuCommands[4]) { MM_End(); }
        }
    }

The MM methods are empty currently, that is where the enabling/disabling will take place. How do I enable or disable the GameObjects inside the MenuSwitch script? The objects are referenced in that script via the Unity inspector.

CodePudding user response:

You could create an empty object and call it MenuManager, attach the MenuScript to it. Then in a script you want to call MenuSwitch functions from declare a class member like so:

public MenuSwitch menuManager;

Drag and drop the MenuManager empty gameObject in the inspector and then you can easily use all the public functions from MenuSwitch script by just calling them from the menuManager variable like so:

menuManager.MainMenu();
menuManager.OptionsMenu();

I hope it was helpful :)

  • Related