- Home /
Not possible
Unityscript: Possible to access static variables dynamically?
Hi, I'm just starting with Unity and I'm struggling to access static variables like Vector3.up, Vector3.down dynamically. I want to check possible collisions via Raycast in a loop, so this is what I ended up with:
public var rays : Hashtable = new Hashtable();
function Update() {
var directions = ["forward", "left", "right", "up", "down"];
for(var dir : String in directions) {
var tmpRay : RaycastHit;
var tmpBool : boolean = Physics.Raycast( transform.position,
transform.TransformDirection(Vector3[dir]),
tmpRay,
1 );
rays[dir] = tmpRay;
rays['is'+dir] = tmpBool;
}
}
When I try to access the static variables of Vector3 (forward, left etc.) via iteration (I tried Vector3[dir]
for this), I get:
BCE0048: Type 'System.Type' does not support slicing.
In Javascript this would work.
Is there any way to access static variables like this?
I'm beginning to wonder if there is any reason to use UnityScript if I can't play with objects like I'm used to in Javascript. Thanks for any advice!
Edit: from what I can tell after a few hours with UnityScript: normal loose Javascript objects don't work at all in Unity. I have to get my head around Unitys data structures. Correct me if I'm wrong :)
Answer by tanoshimi · Apr 12, 2015 at 09:34 PM
Don't use UnityScript myself, but would this not work?
function Update() {
var directions = [Vector3.forward, Vector3.left, Vector3.right, Vector3.up, Vector3.down];
for(var dir : Vector3 in directions) {
var tmpRay : RaycastHit;
Physics.Raycast( transform.position,
transform.TransformDirection(dir),
tmpRay,
1 );
rays[dir] = tmpRay;
}
}
Hey, thanks for taking a look! Apparently reading the values later like rays[Vector3.down]
does not work this way. (Writing them in line 9 throws no Errors though O.o)