Problem creating list of objects

Hi all,

I’m having problem with creating simple script that just adds spheres on left mouse click and deletes them on right click. I get this error: “NullReferenceException: Object reference not set to an instance of an object SphereCreator.Update () (at Assets/SphereCreator.js:18)”. What is wrong?

Here is the script:

var sphere : GameObject;
var sphereCounter : int = 0;
var sphereArray : Array;

function Start () {
	
}

function Update () {

	if(Input.GetKeyDown(KeyCode.Mouse0))
    {
        sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        sphereCounter++;
        sphere.transform.position = Vector3(0, sphereCounter, 0);
        sphereArray.push(sphere);
    }

	if(Input.GetKeyDown(KeyCode.Mouse1))
    {
        if(sphereCounter>0) Destroy(sphereArray[sphereCounter-1]);
    }
    
}

var sphere : GameObject;
var sphereCounter : int = 0;
var sphereArray : Array;

function Start () {
 sphereArray = new Array ();
}
 
function Update () {
 
    if(Input.GetKeyDown(KeyCode.Mouse0))
    {
        sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        sphereCounter++;
        sphere.transform.position = Vector3(0, sphereCounter, 0);
        sphereArray.push(sphere);
    }
 
    if(Input.GetKeyDown(KeyCode.Mouse1))
    {
        if(sphereCounter>0) Destroy(sphereArray[sphereCounter-1]);
    }
 
}

I would suggest:

var sphereCounter : int = 0;
var sphereArray = new Array();

if(Input.GetKeyDown(KeyCode.Mouse0))
    {
        var sphere : GameObject = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        sphereCounter++;
        sphere.transform.position = Vector3(0, sphereCounter, 0);
        sphereArray.Push(sphere);//capital P in push.
    }