- Home /
Converting 'char' to the name of a variable
Hey, I've been searching around to find an example of what I'm looking for, but with no avail.
What I want to do is name a variable out of characters or using a string like this:
transform stuffA; transform stuffB;
void Start () { Do("A"); }
void Do (string test) { stuffB = "stuff" + test; }
Of course, the above doesn't work. The console tells me that it can't convert 'char' to a transform.
I've seen some stuff about Hash tables, but I'm unsure how to use them for this.
Thanks
-AJ
Answer by DaveA · Mar 15, 2011 at 08:22 AM
This depends on what you want Do to do. If you had a game object named stuffB you could GameObject.Find it like that. Otherwise, look into 'eval'
I actually want to name the variable, not find it. I'll have a look at 'eval' thanks!
-AJ
Tho variables created with 'eval' are only in scope for the code passed to it per-invocation. So put as much code as you need to execute in that call. Beyond that, I don't know of any way to create variables on the fly
I don't need to create the variable, so this should be fine :)
Where can I find examples of eval? I have no clue how to use it :P
A hash table (or a map, or dictionary) can allow you to to pair up objects and names so you can look them up using a string/char. It's a much more common approach than directly converting a string into a variable.