- Home /
Error: CS0200 | Read only array?
error CS0200: Property or indexer `System.Array.Length' cannot be assigned to (it is read only)
Anybody can answer what is and how i can sove it? public float[] floorsSeed;
void Awake () {
//DontDestroyOnLoad ();
}
// Use this for initialization
void Start () {
floorsSeed.Length = Random.Range (5, 10);
if (floorsSeed.Length == 5) {
floorsSeed[1] = Random.Range (0000000001, 9999999999);
floorsSeed[2] = Random.Range (0000000001, 9999999999);
floorsSeed[3] = Random.Range (0000000001, 9999999999);
floorsSeed[4] = Random.Range (0000000001, 9999999999);
floorsSeed[5] = Random.Range (0000000001, 9999999999);
} else if (floorsSeed.Length == 6) {
floorsSeed[1] = Random.Range (0000000001, 9999999999);
floorsSeed[2] = Random.Range (0000000001, 9999999999);
floorsSeed[3] = Random.Range (0000000001, 9999999999);
floorsSeed[4] = Random.Range (0000000001, 9999999999);
floorsSeed[5] = Random.Range (0000000001, 9999999999);
} else if (floorsSeed.Length == 7) {
floorsSeed[1] = Random.Range (0000000001, 9999999999);
floorsSeed[2] = Random.Range (0000000001, 9999999999);
floorsSeed[3] = Random.Range (0000000001, 9999999999);
floorsSeed[4] = Random.Range (0000000001, 9999999999);
floorsSeed[5] = Random.Range (0000000001, 9999999999);
floorsSeed[6] = Random.Range (0000000001, 9999999999);
floorsSeed[7] = Random.Range (0000000001, 9999999999);
} else if (floorsSeed.Length == 8) {
floorsSeed[1] = Random.Range (0000000001, 9999999999);
floorsSeed[2] = Random.Range (0000000001, 9999999999);
floorsSeed[3] = Random.Range (0000000001, 9999999999);
floorsSeed[4] = Random.Range (0000000001, 9999999999);
floorsSeed[5] = Random.Range (0000000001, 9999999999);
floorsSeed[6] = Random.Range (0000000001, 9999999999);
floorsSeed[7] = Random.Range (0000000001, 9999999999);
floorsSeed[8] = Random.Range (0000000001, 9999999999);
} else if (floorsSeed.Length == 9) {
floorsSeed[1] = Random.Range (0000000001, 9999999999);
floorsSeed[2] = Random.Range (0000000001, 9999999999);
floorsSeed[3] = Random.Range (0000000001, 9999999999);
floorsSeed[4] = Random.Range (0000000001, 9999999999);
floorsSeed[5] = Random.Range (0000000001, 9999999999);
floorsSeed[6] = Random.Range (0000000001, 9999999999);
floorsSeed[7] = Random.Range (0000000001, 9999999999);
floorsSeed[8] = Random.Range (0000000001, 9999999999);
floorsSeed[9] = Random.Range (0000000001, 9999999999);
} else if (floorsSeed.Length == 10) {
floorsSeed[1] = Random.Range (0000000001, 9999999999);
floorsSeed[2] = Random.Range (0000000001, 9999999999);
floorsSeed[3] = Random.Range (0000000001, 9999999999);
floorsSeed[4] = Random.Range (0000000001, 9999999999);
floorsSeed[5] = Random.Range (0000000001, 9999999999);
floorsSeed[6] = Random.Range (0000000001, 9999999999);
floorsSeed[7] = Random.Range (0000000001, 9999999999);
floorsSeed[8] = Random.Range (0000000001, 9999999999);
floorsSeed[9] = Random.Range (0000000001, 9999999999);
floorsSeed[10] = Random.Range (0000000001, 9999999999);
}
}
Answer by Hellium · Feb 21, 2017 at 07:51 PM
void Start () {
floorsSeed = new float[Random.Range (5, 10)];
for( int i = 0 ; i < floorsSeed.Length ; ++i )
floorsSeed[i] = Random.Range (0000000001, 9999999999);
}
It works properly but can you explain what have you done? because I am a little bit noob program$$anonymous$$g and I want to learn
Just to be clear here, arrays can't be "resized", An array has a fix size and you have to specify it when you create the array. If you need a larger array you have to create a new array. If you want to "keep" the old elements you would need to copy them over from the old array.
That's why the "Length" property is read-only. You can only get the length.
Also note that array indices start at "0", not at "1". The largest index is "Length-1". So an array with 10 elements (Length == 10) has the elements from 0 to 9
floorsSeed = new float[Random.Range (5, 10)];
Instantiates a new array of a size between 5 and 9 (10 is excluded here).The values of the array is initialized with the default value of a float : 0
for( int i = 0 ; i < floorsSeed.Length ; ++i )
floorsSeed[i] = Random.Range (0000000001, 9999999999);
Here, you loop through the whole array (between 0 and the length of the array) and you replace the value of each element by a random value between 1 and 9999999999.
Your answer
Follow this Question
Related Questions
C# Convert json arrays to unity arrys 3 Answers
C# Converting json arrays using JsonUtility, 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Generate the same 'random' number sequence from a seed 1 Answer