How can I change the type of a variable in unity-js? I didn’t find anything about it on the forums. If there is no way: Is it possible to “kill” a variable so I can re-declare it afterwards? I want to reuse the name but change the content. Thanks for help in advance.
Declare it as System.Object and u can cast it to any valid(serilizable) c# type! Object would refer to UnityEngine.Object, you should declare it a System.Object. you can jus assign what ever datatype to the variable of type object!! to get the current type use GetType fuction. For more info go this page.The snippet could be like this
var obj:System.Object;
obj=45;
print(obj.GetType());
obj=45.8f;
print(obj.GetType());
obj=true;
print(obj.GetType());
You can check teh results urslef!! Hope it helps
var vartype : object;
switch(choice) {
case 0: vartype = new float; break;
case 1: vartype = new Vector3(); break;
case 2: vartype = new string; break;
}
new creates an object of a given type and returns a reference to it - which in this case would be assigned to vartype. Since declaring variables inside switch statements is what is giving you trouble, this avoids it. However, I’d perform all the operations on that variable inside the switch since telling the types apart is already handled there for you.
It’s best to just find another name to use. But if you don’t want to do this, you can always try to separate the variables into blocks. Variables declared inside blocks will not affect future declarations outside of that block.
...
// Block one
{
var MyVariable : Mesh;
...
}
// Block two
{
var MyVariable : GameObject;
...
}
...