Why does this passed Array cause errors in a function?

function Afunction(x:String,y:int){//THIS ALL WORKS WITH TheArray being directly called
var Myoutput = new String[TheArray[y].length];
Myoutput[0] = x + " " + TheArray[y][0];
for(var i : int = 1; i<(TheArray[y].length);i++){
Myoutput = SomeOtherArray[0][ConvertToSomething(x,TheArray[y])];
}
return Myoutput;
}

function AMoreDynamicfunction(x:String,y:int,ThePassedArray:Array){//This throws errors with ThePassedArray being seen as some object
var Myoutput = new String[ThePassedArray[y].length];
Myoutput[0] = x + " " + ThePassedArray[y][0];
for(var i : int = 1; i<(ThePassedArray[y].length);i++){
Myoutput = SomeOtherArray[0][ConvertToSomething(x,ThePassedArray[y])];
}
return Myoutput;
}
private var TheArray = [
[“word”,“1”,“2”,“3”,“17”,“2”],
[“words and stuff”,“1”,“2”,“3”],
[“another word”,“1”,“2”,“3”,“4”]];
The compiler error happens before I even get to set up a call to the second (more dynamic) version.
The first function runs perfectly but I wanted to create a similar function using only a different array so I thought it would be great to only need one function and make the array a passed variable so I can just send any array to the one new function. This however gives these errors:
**‘length’ is not a member of ‘Object’.
Type ‘Object’ does not support slicing.**
Is there any way to do this in UnityScript (js) or am I going to have to make the same function again with a different array directly called inside like the first one? I could probably copy the array to another array right before the function call and then directly call that changing array in one function… but isn’t that the whole point of passing an array to a function? Why is this not working?
P.S. in case people wonder TheArray[y].length is correct, It is not meant to be TheArray.length. I am specifically doing this to check the length of an array within an array which works fine in the first function. The first function is 100% working.

Can you show the code where you define "TheArray" and where you are calling "AMoreDynamicfunction"?

–

2 Answers

2

I think the problem is when you pass the Type " :Array" in the function parameters of

function AMoreDynamicfunction(x:String,y:int,ThePassedArray:Array)

it doesn’t recognize the Type you are trying to access.

The Array seems to return instances of type Object, so you need to typecast the object to type Array to use it as an array.

function AMoreDynamicfunction(x:String,y:int,ThePassedArray:Array){//This throws errors with ThePassedArray being seen as some object
    var innerArray = ThePassedArray[y] as Array; // type cast object to array
    var Myoutput = new String[innerArray.length];
    Myoutput[0] = x + " " + innerArray[0];
    for(var i : int = 1; i<(innerArray.length);i++){
      Myoutput _= SomeOtherArray[0][ConvertToSomething(x,innerArray*)];*_

}
return Myoutput;
}

Very good! Thank you. I wonder why this is an issue though. I figured it was getting passed into it through its parent array so it would remain intact. Interesting! This should help others with the same unityscript oddity with passing arrays of arrays. If anything, I need to remember that if this sort of issue happens anywhere else, I should force type cast on thing in question. Thanks again!

–

Well what I did was put as a child of TrackingSpace not OVRPlayerController. And deactivate where it says Hmd reset Y and it was all and it worked. But thanks for the advice.

–