Pushing to arrays?

How come this works?

var array : int[];
var attempt = 3;
var n = 5;
var i : int = 5;


function Start (){

array = new int[10];
array *= attempt;*

}

But this doesn’t?
var array : int[];
var attempt = 3;
var n = 5;
var i : int;

function Start (){
for (var i = 0; i < 5; i++) {
array = new int[10];
array = attempt;
}
}

1 Answer

1

Because you’re declaring the array over and over in the for-loop. Lift out the array = new int[10]; to before you initialize the loop.

Also try to use #pragma strict as religiously as possible so you'll get more information about the errors before they strike.

What is #pragma strict?

#pragma strict enables strong typing and some other stuff which typically works in "JavaScript/UnityScript" but don't in C# for example.