- Home /
Dynamic variable name c#
Hey all
I have a it of an issue with a couple of my scripts
What I need is a script with my variables which are static, and a second generic script to access them
The generic script has to be universal so I can attach to various objects and access different variables from script 1
Example: script1 has 3 variables
public static float var1 = 1; public static float var2 = 2; public static float var3 = 3;
Script2 access one of those variable
string dynVar; void Start () { float x = script1.var2; }
Now I know that code won't work as its just as an example
I want to be able to change the name of the variable in the inspector
Hope this makes sense, also if it makes a difference its planned for mobile device
Answer by NeverHopeless · Jul 02, 2015 at 10:58 AM
Probably you might need to have a look at System.Reflection
namespace. With this you can read the fields and properties of the class. Parametrized the variable name in the inspector and get the relevant field using reflection.
It is a possiblility, but if it is also possible for you, you can use Enums to access the other/remaining variables of Script1
based on different enum values.
If you keep things simple, it is easy to troubleshoot the problems.
Your answer
Follow this Question
Related Questions
Yielding for a changing amount of time? 2 Answers
Changing game object based on variable state? 1 Answer
Variable Resolution 2 Answers