Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

SetActive Doesnt Work

Discussion in 'Scripting' started by cate5171, May 24, 2020.

  1. cate5171

    cate5171

    Joined:
    Jan 5, 2020
    Posts:
    101
    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?

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class ToggleGameObjectButton : MonoBehaviour
    5. {
    6.     public GameObject objectToToggle;
    7.     public bool resetAfterClick;
    8.  
    9.     void Update()
    10.     {
    11.         if (objectToToggle.activeSelf && Input.GetButtonDown(GameConstants.k_ButtonNameCancel))
    12.         {
    13.             SetGameObjectActive(false);
    14.         }
    15.     }
    16.  
    17.     public void SetGameObjectActive(bool active)
    18.     {
    19.         objectToToggle.SetActive(active);
    20.  
    21.         if (resetAfterClick)
    22.             EventSystem.current.SetSelectedGameObject(null);
    23.     }
    24. }
    25.  
     
  2. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    Is it running the function? Use debug.log in the function.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    For Update() to run, whatever GameObject this script is on also has to be already active.
     
    leftshoe18 likes this.
  4. ProntName

    ProntName

    Joined:
    Apr 27, 2020
    Posts:
    15
    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.
     
    leftshoe18 likes this.