- Home /
Cannot read Array Length
Hellow, It would seems I cannot read the length of an array when it's created this way.
var myArray = new Array();
When I do
print (myArray.lenght);
it returns me this error.
Null
UnityEngine.MonoBehaviour:print(Object)
cameraRail:printDistances() (at Assets/Scripts/Camera/cameraRail.js:35)
UnityEngine.Component:SendMessage(String)
cameraRail:calDistances() (at Assets/Scripts/Camera/cameraRail.js:29)
UnityEngine.Component:SendMessage(String)
cameraRail:Start() (at Assets/Scripts/Camera/cameraRail.js:11)
Any idea what is going on?
I assume that's a typo there. myArray.lenght shouldn't even get past the compiler. Are you putting anything in the array before printing its length?
Answer by sriram90 · Jan 09, 2012 at 09:14 AM
Here it is your answer:
using C# :
private int[] testArray = new int[5];
print(testArray.Length);
using JS:
public var testArray = Array(5);
print(testArray.length);
I'm pretty sure it's the other way around. http://unity3d.com/support/documentation/ScriptReference/Array.html
i nothing said as wrong here...it's the way to get length in simple 2 lines of script
+1. I confirm sriram90 code is correct. Note that those are two different types of Array. The builtin array version as displayed in the C# example also exist in JS flavor; var testArray:int[] = new int[5]; All JS Array uses .length
Your answer
![](https://koobas.hobune.stream/wayback/20220613062810im_/https://answers.unity.com/themes/thub/images/avi.jpg)