Q:

A way to avoid coding super specific function using the new Unity UI

I need to rework a UI created using the old UI.
I can't change the architecture of the program. The program uses a lot of static variables and my UI need to access them and edit them (i know static variables are quite bad but can't rework the whole architecture like i said).
Currently i'm doing something like this :
 public void SetBool()
         {
             UIData.SomeBool = SomeToggle.isOn ;
         }

         public void SetAnotherBool()
         {
             UIData.SomeOtherBool = SomeOtherToggle.isOn ;
         }

...
So i have a function for each static variable i want to edit associated with a specific toggle, this is ugly. How can i create a generic function to edit bool using toggle and slider to edit float values.
I thought about something like this :
 public void SetBool(bool boolToSet)
          {
              boolToSet = gameObject.GetComponent<Toggle>.isOn;
          }

And then add listener to the toggle for the event OnValueChanged and this function with the bool i want to change as parameter. But I have heard that calling GetComponent is bad for the performance.
I wanted to know if u had any better idea. Maybe i'm missing something important i'm quite to new to unity and c#.
Thanks
Sorry for my poor english.

A:

GetComponent performance isn't that bad as it's just searching a list on a GameObject. It's just not advisable to call it every frame in many places without caching. For a user initiated UI action it will have basically no performance impact. FindObjectOfType on the other hand searches the whole hierarchy so is quite slow, and GetComponentsInChildren may be slow if the branch of the hierarchy you are searching is large.
Still it is good practice to cache your own component references in Awake(). I do internal state configuration in Awake and any external connections and dependencies in Start to make sure an object is properly initialized before speaking to the outside world.
If you want to pass in a Value type and change the value you need to pass by reference:
private Toggle toggle;
public void Awake() {
   toggle = GetComponent<Toggle>();
}

public void SetBool(ref bool boolToSet) {
    if (toggle != null) {
        boolToSet = gameObject.GetComponent<Toggle>.isOn;
    }
}

Then from wherever you call SetBool:
SetBool(ref myBoolField);

If you can guarantee a Toggle component exists you can remove the null check from SetBool. RequireComponent can help with this but it only stops you assigning something in the inspector... if code changes or you construct via script I don't think it does anything.

