- Home /
String + Variable as variable name?
I'm trying to create a system in which I create a new variable every time a function is called. For example, click something once and a variable called 'test1' is created. Click a second time and 'test2' is created.
I've come up with this so far, but it is not working properly:
Hash["test" + stepNumber] = int();
Answer by Mike 3 · Jul 15, 2010 at 01:53 AM
The only bit wrong with your code is the = int(); bit
Swap that with a normal assignment and it should work fine
e.g.
Hash["test" + stepNumber] = 5;
This is, however, assuming Hash is a Hashtable variable of some kind (which it does sound like)
Answer by Cyclops · Jul 15, 2010 at 01:36 AM
Use String.Format to combine the two:
var fullStep : String;
var stepNumber : int = 2;
fullStep = String.Format("test{0}", stepNumber);
Debug.Log(fullStep); // prints test2
Hash(fullStep) = stepNumber;
Your answer
Follow this Question
Related Questions
How to modify variables from scripts in standard package? 2 Answers
How to make variable public "In whole script" from update function? 2 Answers
How to use Float as Name in GameObject.Find command? 1 Answer
Changing variable from another script 1 Answer
Javascript not being updated, variables being overridden, but C# is fine 1 Answer