- Home /
Find variable in another script using a string
Is it possible to access a variable in another script by using a string in, say a function argument?
like:
//Script 1
string Test1;
//Script 2
void FindVariable(string StringToFind){
//Find the variable
}
Not for nothin, but when you find yourself needing to do this, you're probably not following strong design practices. It can be hard to avoid "spaghettification," (classes beco$$anonymous$$g heavily coupled and interdependent) but learning to cope with complexity is part of the learning process.
Often interfaces and delegates can replace the need you're addressing here. Or just hard-code it. If you really want to learn to accomplish finding-variables-by-name, you'll (probably?) need to use reflection.
Answer by ifisch · Mar 08, 2015 at 09:20 AM
Sorry to be a dick, but I can't imagine you've chosen the best way to do things.
In general, you want to use as few strings as possible in your code. They're hard to track down, and they won't throw errors if you misspell them, meaning the game will be broken and you won't know why.
Your answer
Follow this Question
Related Questions
Change multiple variables with function argument 2 Answers
C# Variable 4 Answers
Distribute terrain in zones 3 Answers
Calling non applied scripts 3 Answers