- Home /
Change multiple variables with function argument
I am using the new UI Button. I want a function to put under the OnClick Component. I want a function that I can reuse and just change 1 thing. Thats what I'm trying to do.
How do I change a variable in another script by, with a single function?
like:
void Change(ThingToChange){
VariableScript.ThingToChange = "Something";
}
Not:
void ChangeTest1(){
test1 = "blabla";
}
void ChangeTest2(){
test2 = "blabla2";
}
And so on. That I what is don't want to do! I want to have only 1 void/ function that I can reuse.
What I want is:
A script with multiple string in $$anonymous$$ultiple buttons I can press to change the strings in the first script. Using a single void/ function for every button.
Thats it. Is there a way to do this?
Answer by d2 · Mar 09, 2015 at 03:37 PM
Maybe you can use Generics:
https://unity3d.com/es/learn/tutorials/modules/intermediate/scripting/generics
Answer by alok-kr-029 · Mar 09, 2015 at 10:42 AM
your question is not to clear but I guess you mean somthing like this
public void ChangeTest( string button_fun)
{
switch(button_fun)
{
case "test1":
print("dosmthing");
break;
case "test2":
print("dosmthing2");
break;
}
}
by passing the parameter to the button and making the case you can reuse the function One single function for all the button
This is the base code of what is consider spaghetti code. You will end up with one function doing all kind of things unrelated to each other. Debugging becomes a nightmare and you can forget about efficient scalability.
Better to have a lot of small functions that do only what they are meant to. It may seem like annoying at first because you need to drag them all but when a problem occurs, you need exactly where and why.
Your answer
Follow this Question
Related Questions
Find variable in another script using a string 1 Answer
string + int = variable 2 Answers
UI button change variables in another script 1 Answer
Multiple Cars not working 1 Answer