Storing result of Instantiate - wierdness

Hi,
Can anybody explain why this code results in 10 objects on the screen (as I would expect):

 function drawBricks() {
	for(var x=0; x<10; x++) {
		Instantiate(Resources.Load("myPrefab"), 
			new Vector3((x * 2f) - 5f, 0, 0), 
			Quaternion.identity);
	}
 }

… but this code results in me only getting one object created on the screen? (the first one):

function drawBricks() {
 	var brick:Rigidbody;
	for(var x=0; x<10; x++) {
		brick = Instantiate(Resources.Load("myPrefab"), 
							new Vector3((x * 2f) - 5f, 0, 0), 
							Quaternion.identity);
	}
 }

I don’t understand why storing the result in a local variable matters? I thought perhaps if I don’t store a reference to the instantiated object then perhaps it will get garbage-collected or something, but that doesn’t explain (in my head) why the first one remains but none of the subsequent ones get created.

Thanks for any help. I find this very confusing!

Because in your second example, you’re trying to assign the result of Instantiate (a Gameobject) to brick, which is a Rigidbody, causing an error that stops your code execution.

Change line 2 to:

var brick: Gameobject;