# Why does this passed Array cause errors in a function?

**URL:** https://discussions.unity.com/t/why-does-this-passed-array-cause-errors-in-a-function/186798
**Category:** Questions & Answers
**Created:** [May 3, 2017, 11:41am UTC](https://discussions.unity.com/t/why-does-this-passed-array-cause-errors-in-a-function/186798 "2017-05-03T11:41:39Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![allenrobot](https://avatars.discourse-cdn.com/v4/letter/a/e68b1a/32.png) [@allenrobot](https://discussions.unity.com/u/allenrobot)
#### Post date: [May 3, 2017, 11:41am UTC](https://discussions.unity.com/t/why-does-this-passed-array-cause-errors-in-a-function/186798/1 "2017-05-03T11:41:39Z")

</div>

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._

---

<div class="post-metadata">

### Author: ![zacb\_dz\_123](https://avatars.discourse-cdn.com/v4/letter/z/8e8cbc/32.png) [@zacb\_dz\_123](https://discussions.unity.com/u/zacb_dz_123)
#### Post date: [May 4, 2017, 1:27pm UTC](https://discussions.unity.com/t/why-does-this-passed-array-cause-errors-in-a-function/186798/2 "2017-05-04T13:27:47Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![bobisgod234](https://avatars.discourse-cdn.com/v4/letter/b/b77776/32.png) [@bobisgod234](https://discussions.unity.com/u/bobisgod234)
#### Post date: [May 4, 2017, 4:25am UTC](https://discussions.unity.com/t/why-does-this-passed-array-cause-errors-in-a-function/186798/3 "2017-05-04T04:25:21Z")

</div>

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;_  
_}_
