C# - Can a public function argument be a selection of public static variables?
Just a quick question to know whether or not this is possible.
I like to keep the habit of being DRY with my coding, so i don't like to recreate the same function, just to change one variable name. For example, I have a lot of buttons, and each one does the same basic function but for different GameObjects, example:
public void pressedSoundOptionButton() {
if (soundsOption) {
soundsOptionMarker.SetActive (false);
} else if (!soundsOption) {
soundsOptionMarker.SetActive (true);
}
I have buttons that attach to these public functions, But what i'd like is that each button can use the same function, but pass an argument telling it which variable and GameObject to check for. (Maybe two arguments)
IS it possible to do something like:
public void pressedOptionButton (bool.name BooleanToUse) {
//do stuff with the boolean variable given to this function
}
Obviously i can't do (string varName), But I could always pass a string, and then lay out a long list of if's saying if (string == "var1") { booleanTouse = var1; But i feel like that would be bad practice.
(Also I'm new to C#) Whats the genius way of doing this?
This is not a Unity question, but a pure C#-related one, and thus, does not belong to Unity Answers.
One word to answer you though : Reflection
FAQ :
What are the guidelines for writing good questions?
We want Unity Answers to become a comprehensive database of questions and answers related to Unity.
well perhaps it could be a unity question, since I'm parsing the arguments through the unity button/inspector menu, and this could be related if Unity has some - if any - way to parse those arguments in a unique way, like a drop down menu to select variables within that script to be parsed. (But granted, i see that this is unlikely, and probably just a C# question - doesn't hurt to ask though)
Your answer
Follow this Question
Related Questions
error CS8025: Parsing error: dont know what to do here 1 Answer
How to remotely (with a different C# script or js) activate function from the main C# script? 1 Answer
Is it possible to make custom automatically-called methods like Start and Update? 0 Answers