SetActive Doesnt Work

I have this script which would make a menu appear but when i try to exit it, it doesnt respond. Like it does nothing. There is no errors at all and i cant find a answer for this. Any help?

using UnityEngine;
using UnityEngine.EventSystems;

public class ToggleGameObjectButton : MonoBehaviour
{
    public GameObject objectToToggle;
    public bool resetAfterClick;

    void Update()
    {
        if (objectToToggle.activeSelf && Input.GetButtonDown(GameConstants.k_ButtonNameCancel))
        {
            SetGameObjectActive(false);
        }
    }

    public void SetGameObjectActive(bool active)
    {
        objectToToggle.SetActive(active);

        if (resetAfterClick)
            EventSystem.current.SetSelectedGameObject(null);
    }
}

Is it running the function? Use debug.log in the function.

For Update() to run, whatever GameObject this script is on also has to be already active.

1 Like

Create an empty game object and place the script on that object as a sort of “MenuManager”, that way, you won’t run into the problem Kurt mentioned.

1 Like