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

1 Answer

1

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'

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

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.