- Home /
String to Var
My script shall provide a text-input-field in inspector to type in a variable-name.
How do I convert this string to a variable I can use in my script afterwards?
Answer by Eric5h5 · Dec 16, 2011 at 04:52 AM
Since you're using JS, you can use eval(), as long as it's not a webplayer or iOS/Android.
It's a language feature rather than a Unity feature.
var n1 = 5;
var n2 = 9;
var var1 = "n1";
var var2 = "n2";
function Start () {
eval("print (" + var1 + " * " + var2 + ");");
}
I should amend the above; actually it works fine in a webplayer (was thinking of something else, sorry). It doesn't work on iOS because of the way code is compiled.
Answer by jahroy · Dec 16, 2011 at 02:51 AM
You'll have to use reflection for that:
I recommend restricting the user's input by presenting them with a list of choices, rather than trying to process their raw input. That being said, I don't really know what you're trying to do....
It would be easier for us to help you if you tell us what you're trying to do.
I knew this answer would come. There is nothing more you can understand: The script is doing some math, someone can use for many purposes. To not having the need to open the editor, someone shall be able to define two input and one output-variable. I'm using javascript.
Well... If the user must type the name of a variable (not sure why that would be), then you'll have to use reflection.
I have a feeling there's a much easier approach, but I don't understand what you're saying.
$$anonymous$$y guess is a Hashtable, Dictionary, or Array is the solution to your problem.
Are you familiar with these data types?
You would use a Hashtable, for example, if you needed to keep track of a bunch of phone numbers and wanted to associate each one with a name.
In that situation, the name is referred to as the "key" and the phone number is referred to as the "value". The keys and values of a Hashtable can be any type: ints, floats, strings, Transforms, GameObjects, etc...
$$anonymous$$aybe the values the user is entering into the TextField could be keys in a Hashtable or Dictionary.
Not sure... Just trying to help.
Which of the many reflection-examples is the proper one anyway? I know this answer was given in another threat already. The unity-documentation knows of many functions looking related. For instance (without knowing what is usefull):
The Objects-page has the reverse-function: Object.ToString. Also, this answer claims to know a way to list variables which could be usefull to make a drop-down-list to choose from: How to create a collapsible variable list
The variable-page in the unity-documentation says that all variables are member-variables by default which are available in inspector and even get saved with the project. So how do I access them from within inspector?
To access the variables of a script in the Inspector, you take the following steps:
create a script with some variables
attach the script to an object in the scene (drag/drop)
select the object from the scene (left-click on it)
The Inspector is typically on the right side of the screen (otherwise you can get to it from the Window menu).
When you select the object that has your script attached, you should see a slot in the Inspector for every public variable in your script (they're public by default).
Depending on the type of the variable you can either edit it directly in the Inspector or drag/drop objects onto its slot.
If you use the example below, you'll notice that you can edit the Color in the Inspector with all kinds of neat input methods. You can also drag a $$anonymous$$esh/Transform onto the slot named Some Transform.
Here is a simple example script you could use:
var someString : String;
var someInt : int;
var someFloat : float;
var someColor : Color;
var someTransform : Transform;
function Update ()
{
/* make sure string isn't null before accessing it */
if ( someString != null ) {
Debug.Log("the string: " + someString);
}
/* ints and floats are never null - no need to check */
Debug.Log("the int: " + someInt);
Debug.Log("the float: " + someFloat);
Debug.Log("the color: " + someColor);
}
Your best bet is to spend some time learning the basic principles of scripting with Unity. It is very very clear that you do not know what you want.
This question is accomplishing nothing and should be deleted. Unity Answers is not meant to provide a tutorial zone for individual users. It's supposed to be a place where you can ask questions that will be useful to many people.
We've written all this junk and I still have absolutely no idea what you're trying to achieve.
If you want to quickly figure out the answer to your problem, you should provide a detailed, english description of what you want to do. Explain what the input from the user should be and what you'll do with it. If you don't want to tell us, it will be almost impossible for us to help.
If what you're working on is a big secret, you'll have to either figure it out yourself or hire someone to help.
Right now you're mis-using Unity Answers and won't get any help.
Your answer
Follow this Question
Related Questions
Have a GUI.Button string include a variable as part of the string? 0 Answers
How to Convert a Variable Name to a String? 2 Answers
variables as GUI 1 Answer
Inputfield set as Variable 1 Answer