- Home /
Redim a variable
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.
I really don't think you can do that(I am sure someone will correct me, though). However variables used inside one function cannot be accessed in another, therefore you can re-declare it in another function.
However I am a little unsure whether you can reuse the same name, in different functions. I can't test that right now I am sorry.
What's about enum-vars? I found many examples of usage. But nobody seems to define the types of the sub-vars. Is it possible nevertheless?
what exactly are you trying to change about the variable? you can always try and typecast if it's within the same realm.
$$anonymous$$ore importantly, why? Why do you want to do something which is unequivocally dangerous?
I want to change the type. What is typecast and what is realm?
Answer by flamy · Dec 21, 2011 at 06:23 AM
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 :)
One more question: When assigning
comptype = new Transform();
the compiler says:
"BCE0120: 'UnityEngine.Transform.constructor()' is inaccessible due to its protection level"
All other var-types make no trouble.
that is simple
while assigning the transform do something like this
obj= transform as UnityEngine.Object;
this would do it!
It does !! [Edit:]...not! I now get the same error-message.
dude are u trying to use new Transform() or something like this?!?! it is not possible to do in unity!! because you cannot create transform, you can only access transform of a gameObject!... if u want to access the transform of the current object you can call like this
obj=transform as UnityEngine.Object;
if you want to access some other object you have to use transform.Find or a public variable and attach it from editor!!
Answer by Zergling103 · Dec 21, 2011 at 06:27 AM
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.
ya also you can use UnityEngine.Object as its type (or just Object no difference) and it doesnt the exact same thing, wat System.Object...
because UnityEngine.Object is just an alias of System.Object.
Answer by Zergling103 · Dec 21, 2011 at 06:06 AM
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;
...
}
...
It looks exactly as what I wrote. What am I missing here? The example I posted is using blocks.
You can't use '$$anonymous$$yVariable' outside of the brackets in which it is declared. I still want to know why you want to do this. It is never a good idea, and there is almost certainly a better way to achieve what you actually want to do here (since it's a means to an end, rather than an end in and of itself)
Also, declared this way, you cannot use the variable outside of individual blocks. If you need to do that then you'd have to try the method I mentioned above using objects and "new".
But honestly I'd revise my script to avoid this issue altogether.