Why is array.Length read only?

I’d like to resize my array of floats, that I create like this:

public float[] MyValues={3.1f,2.4f,1.1f};

with code like this:

  private int MyArraySize=6;
  void Start(){
    MyValues.Length = MyArraySize;
  }

I used this → http://docs.unity3d.com/Documentation/ScriptReference/Array.html
as refference tho it’s not c#…

And now i get this error:

error CS0200: Property or indexer `System.Array.Length' cannot be assigned to (it is read only)

Any sugestions?

You can’t; that’s the Unityscript Array class. It has nothing to do with C# or built-in arrays. Use the MSDN docs (which should also be used if you’re using Unityscript, because all languages use .NET). If you’re resizing arrays a lot, don’t use them, use List, but if you do occasionally resize an array, use Resize.

–Eric

use Array.Resize method. Reffer on MSDN.

I used Array.Resize, worked ok, but now when i’m trying to change the values via checking if its possible, i get soem weird results.
First of all it seems to replace the current values, but at the end of it i get a broken array error. Now when i debuged it, i found some pretty strange things happening, after the 5 value it’s replacing twice and seems like it doesnt know what to replace… it sometimes repalces the previous item … wich is quite not the effect i want haha.

Anyway here is the code i wrote:

	void ReplaceArrayValue(float[] array,int check,int max,float replaceValue)
	{	
		if (array.Length==max)
			for (int i = 0; i < array.Length; i++) 
				if (array[i]==array[check])
				{
					Debug.Log("Replacing " + i + " with "+check+" ("+replaceValue+")");
					array[check]=replaceValue;	
				}
	}

this is the result i get:

well this seems to work:

	void ReplaceArrayValue(float[] array,int check,int max,float replaceValue)
	{	
		if (array.Length==max )
		{
			if (check < array.Length)
			{
				Debug.Log("Replacing " +check+ " ("+replaceValue+")");
				array[check]=replaceValue;	
			}
		}
	}