Problems trying to use .Push() .

Hey,

I’ve been trying to use .Push() in an attempt to store the position of a transform before I destroy it and have run into several problems. First, I get the error “object reference not set to an instance of an object”. This happens every time after the first time an object is selected.

Secondly, I messed something up with the Store() function, but I have no idea what. When it is called, the rest of my program goes to hell. I can’t seem to select or move objects after the first run through and really have no idea why.

Below is a copy of my code and I am hoping someone can set me straight.

static var selectFirst : GameObject;
static var selectFirstParent : GameObject;
static var selectSecond : GameObject;
static var selectSecondParent : GameObject;
var firstPositionStack : Stack;




function Update(){

if (Input.GetMouseButtonDown(0) && clickTime >= .5){ // if button pressed...

var hit: RaycastHit; // cast a ray through the mouse position
     
    if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit)){
      
		switch (clickCount){ // check the state:
        	
        	case 2: // after 2 clicks, restart from the first object
    			first = null;
    			second = null;
        	
        	case 0: // nothing clicked yet: save first object ref
				selectFirst = hit.collider.gameObject;
				selectFirstParent = selectFirst.transform.parent.gameObject;
				first = selectFirstParent.GetComponent(Transform);
				clickCount = 1;
				clickTime = 0.0;
				break;
        		
			case 1: // 1st object already clicked: save the 2nd
				selectSecond = hit.collider.gameObject;
				selectSecondParent = selectSecond.transform.parent.gameObject;
				second = selectSecondParent.GetComponent(Transform);
				clickCount = 2;
				clickTime = 0.0;
				Check();
				Store(first);
				break;
			} 
		}
	}
	
	if (endCount == 1){

		EndGame();
	}	
}




function Store(first){

	firstPositionStack.Push(first);
	Debug.Log(firstPositionStack);

}

You forgot to put ‘break’ under the case 2: segment.

Also you may wish to store positions as Vector3 instead of Transforms. I’m not sure but it’s possible the array will only store the reference to a tranforms, meaning a destroyed transform will become null in the array…just a thought.