Length of Builtin Array in C#

Due to script interoperability problems, the entire project is being converted over to C#. But we ran into a problem with one of the scripts. We’re using builtin arrays so that they are accessible in the editor.

Really simple code to reset to the end of a builtin array. chassisType is the pointer for the array chasses. (During update, if the value changed, it grabs the game object at chasses[chassisType] )

if(chassisType < 0)
     chassisType = chasses.length-1;

But, this throws an error when compiling in C#. The .length property no longer works.

The alternative is:

private const int CHASSES_LENGTH = 5;
public GameObject[] chasses = new GameObject[CHASSES_LENGTH];

. . . 

if(chassisType < 0)
     chassisType = CHASSES_LENGTH -1;

However, if the array length is changed in the editor, the entire thing falls apart.

Is .length really not implemented in C#? Is there a reason?

It’s Length, not length.

–Eric

Thanks. :slight_smile: I really appreciate the quick reply.

There really needs to be a builtin array page in the scripting reference for things like that, since the js version was lowercase (and its the only reference in the docs that I could find).

Even in JS it’s technically Length rather than length, since that’s what .NET uses. However length is also allowed in JS as an equivalent because regular Javascript uses that (and in fact only length can be used for the special Javascript-only arrays in Unity). But you will find only Length used in C# and .NET references.

–Eric