Okay, I guess I'll make it sounds way more complicated than it actually is but here it goes:
I have a built-in array of Strings and then want to retrieve and manipulate the according variables on the script. ie:
var myVar1:int;
var myVar2:String;
var myVar3:float;
var varNames:String[] = new String[3];
//I go in Inspector and set varNames to ("myVar1","myVar2","myVar3"), then:
for(i=0;i<varNames.length;i++){
print(varNames <em>+ " = " + **varNames_.ToVar()**);_</em>
_*}*_
_*```*_
_*<p>Of course .ToVar() do not exist, this is what I need to find.*_
_*While I'm at it: .ToType() is something I could definitly use, too (same pseudo-code).</p>*_
_*<p>Once again: I'm trying to access a variable having only it name as a String.</p>*_
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.