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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Use of Update()

Discussion in 'Scripting' started by mradhi, May 1, 2023.

  1. mradhi

    mradhi

    Joined:
    Mar 26, 2023
    Posts:
    2
    Hi! First time on the forums.
    I am new to Unity and want to have a button enabled/disabled depending on a toggle ("EditMode") state. I have it working, but understand using Update() is not good practice as it is being called every frame so could have some performance issues in the future. Wondering, in such a case as below, how could I write it so that I do not use the Update().

    Code (CSharp):
    1. public class EventLogic : MonoBehaviour
    2. {
    3.     [SerializeField] EditMode EditMode;
    4.     public Button EventBtn;
    5.     private void Start()
    6.     {
    7.         GameObject EditModeObject = GameObject.Find("EditMode");
    8.         if (EditModeObject != null)
    9.         { EditMode = EditModeObject.GetComponent<EditMode>(); }
    10.  
    11.         EventBtn = GetComponent<Button>();
    12.     }
    13.     private void Update()
    14.     {
    15.         EventBtn.enabled = !EditMode.EditModeChanged;
    16.     }
    17.     public void ButtonPressed()
    18.     {
    19.         if (EditMode.EditModeChanged == false)
    20.         { Debug.Log("Button Pressed!"); }
    21.     }
    22. }
    Any help is greatly appreciated!
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    You could probably hook up a event/delegate thing so that whenever the value gets changed, it runs specific code.

    Code (CSharp):
    1. // inside of your EditMode class
    2. public class EditMode : MonoBehavior
    3. {
    4.   public delegate void BoolChange(bool value);  // the signature, it determines what kind of parameters and return value the event has
    5.   public static event BoolChange EditModeChange;  // declares your event, it's static so that other classes can access it easily without a reference to the specific component
    6.   // all your other stuff
    7.   public bool SetEditMode(bool value)  // call this whenever you actually want to update the edit mode, and do anything else in there that you want to happen
    8.   {
    9.     EditModeChange(value);
    10.   }
    11. }
    Code (CSharp):
    1. public class EventLogic : MonoBehaviour
    2. {
    3.     // you don't even need a reference to your EditMode anymore if you're using a static event
    4.     public Button EventBtn;
    5.     private void Start()
    6.     {
    7.         EditMode.EditModeChange += SetButtonEnabled;  // tell the delegate which method to run
    8.         EventBtn = GetComponent<Button>();
    9.     }
    10.     public void SetButtonEnabled(bool value)
    11.     {
    12.         EventBtn.enabled = !value; // set the value here or whatever
    13.     }
    14.     public void ButtonPressed()
    15.     {
    16.         if (EditMode.EditModeChanged == false)
    17.         { Debug.Log("Button Pressed!"); }
    18.     }
    19. }
     
  3. mradhi

    mradhi

    Joined:
    Mar 26, 2023
    Posts:
    2
    Amazing, thank you so much!