There is no "easy" way of doing that and it's not recommended to do such things. In most cases there are better solutions. I don't know what you want to achieve in the end but one way would be to use .NET/MONO Reflection. If you really want to use reflection i would recommend C# and visual studio just because of autocompletion and intellisense.
But as i said that's not the way you should go. Reflection is quite slow and if you don't know what you're doing you can even crash your game. Maybe tell us what exactly you want to achieve and we find the best solution.
Edit
In this example I use 4 scripts (for my predefined property types). You can drag&drop one or more properties to a gameobject and give them a name. I use the component system of unity to search for them. Thanks to the base class “IProperty” you can set the value with get() and set() as string.
// IProperty.js - should not be added to any gameobject, it's just the base class
class IProperty extends MonoBehaviour
{
var Name : String;
function Set(aValue : String) { } ;
function Get() : String { return ""; };
}
// ****************************************************************************
// IntProperty.js
class IntProperty extends IProperty {
var value : int;
function Set(aValue : String) {
value = int.Parse(aValue);
}
function Get() : String {
return value.ToString();
}
}
// ****************************************************************************
// FloatProperty.js
class FloatProperty extends IProperty {
var value : float;
function Set(aValue : String) {
value = float.Parse(aValue);
}
function Get() : String {
return value.ToString();
}
}
// ****************************************************************************
// StringProperty.js
class StringProperty extends IProperty {
var value : String;
function Set(aValue : String) {
value = aValue;
}
function Get() : String {
return value;
}
}
To access the variables:
private var properties : IProperty[] = null;
function GetPropertyByName(aName : String) : IProperty
{
if (properties == null)
properties = GetComponents.<IProperty>();
for (var P : IProperty in properties)
{
if (P.Name == aName)
return P;
}
return null;
}
var health : FloatProperty = null;
function Awake()
{
health = GetPropertyByName("Health") as FloatProperty;
}
function Start()
{
// fast, direct way
health.value = 100.0f;
// slower, dynamic way
GetPropertyByName("Health").Set("43.3");
}
Well, that's just a concept. The function GetPropertyByName should also be boxed in a strategy-pattern and all scripts that have dynamic variables should implement that interface. Finally i didn't turn IProperty into an interface just because it easier with the "Name" var ;). In general the interface should implement a GetName() function. But as i said, it's just a concept.
You almost certainly don't actually want to do that. (Though technically it's possible using reflection or eval().) Generally these sort of situations should use arrays or enums.
– Hagoodwoot! eval(myString) is exactly what I was looking for. Many thanks!
– Manifold