- Home /
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[i] = SomeOtherArray[0][ConvertToSomething(x,TheArray[y][i])];
}
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[i] = SomeOtherArray[0][ConvertToSomething(x,ThePassedArray[y][i])];
}
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 "A$$anonymous$$oreDynamicfunction"?
I added, however..note that first function works 100% and the second array is just like the first one. (An array of arrays of varied length with only string values) Except I have it as a different array because different parts of the program use it separately in a different way than the first array. (Also cleaner and easier that way for stuff down the line). These are not 2 functions running at the same time or in the same program. I was wanting to kill the first one and use the second with different arrays I pass to it.
Answer by bobisgod234 · May 04, 2017 at 04:25 AM
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[i] = SomeOtherArray[0][ConvertToSomething(x,innerArray[i])];
}
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!
Answer by zacb_dz_123 · May 04, 2017 at 01:27 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612121107im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Storing functions in a list/array? 1 Answer
passing a javascript array into a function sorts it? 1 Answer
Passing an Array 1 Answer