- Home /
Array of Transform[] not correctly typed in Javascript
Relevant bits of code:
private var backgrounds:Array[];
function Start() {
backgrounds = new Array[backgroundPrefabs.length];
for(var i:int = 0; i<backgroundPrefabs.length; i++){
backgrounds[i] = new Transform[GetNumBackgroundsNeeded(backgroundPrefabs[i])];
leftMostBG[i] = 0;
for(var j:int = 0; j<backgrounds[i].length; j++){ // MAKE SURE PIVOT IS BOTTOM LEFT
backgrounds[i][j] = Instantiate(backgroundPrefabs[i], new Vector3(xMin +
(backgroundPrefabs[i].GetComponent.<SpriteRenderer>().sprite.bounds.size.x * backgroundPrefabs[i].localScale.x * j), yMin, 0), Quaternion.identity);
}
}
}
So, at this point, as far as I know I have an Array of Transform arrays, but when I do this:
backgrounds[a][b].position.x -=dx * Time.deltaTime * speed;
I get BCE0019: 'position' is not a member of 'Object', which indicates that the Transform arrays are somehow not correctly typed..how can I fix this?
What's the advantage of using List? Is typecasting to Transform very resource intensive?
Array is slow, untyped, and lacking in features. It makes your code worse, and kicks puppies for fun (probably).
Seriously tho, just eli$$anonymous$$ating all the ( as Whatever) would be worth it, even if it did NOT impact performance.
Answer by Benoit Dufresne · Jan 19, 2014 at 02:20 AM
Try
(backgrounds[a][b] as Transform).position.x -=dx * Time.deltaTime * speed;
so THAT'S how you typecast in Javascript..wow I feel silly, I'm used to Java. Is all that typecasting going to be a big performance hit? It's going to be quite a few times per frame.
Like Eric said, you could use generic lists. No type casting, better-looking code and generally easier to work with.
Also as a general rule of thumb: when this guy says something, he's right =D
Your answer
Follow this Question
Related Questions
how would I make an array of different scripts 1 Answer
Load Objects from a specific directory into a list? 0 Answers
2D GameObject Array to 2D List 1 Answer
Is there a way to convert a 2d generic list to a string in unityscript 1 Answer
How to Find all objects with Tag and List their Transforms? 4 Answers