Child gameobject confusion

Hi guys,

I have a bunch of puzzle pieces that come in from Maya (as an fbx). They are parented to a ‘puzzleParent’ object. I want to iterate through all child objects of puzzleParent and store them in an array to manipulate later on.

My code is complaining about not being an instance … NullReferenceException: Object reference not set to an instance of an object … on the line where I’m trying to add the object to my array.

Here is the code and any help would be more than welcome :slight_smile:

private var gamePieces : Array;

function Awake () {

	// FIRST WE FIND THE PUZZLE PARENT OBJECT, TO WHICH ALL PUZZLE PIECES ARE CHILD OBJECTS
	var puzzleParent : GameObject;
    puzzleParent = GameObject.Find("puzzleParent"); 
    puzzle=puzzleParent.transform;
    
    // ITERATE THROUGH CHILD OBJECTS (OF PUZZLEPARENT) AND STORE REFERENCES TO THEM
    for( var aPuzzlePiece : Transform in puzzle ) { 
		var a=aPuzzlePiece.transform;
		gamePieces.Add(a);
	} 
   
	resetPieces();
    
}

Thanks!
Jeff.

gamesPieces is still null (not set to an instance) in your script. You’ve set it’s type but not assigned anything to it.

Try: private var gamePieces :Array = Array();

Gah! I was just getting obsessed with looking in the wrong place for the solution lol

Thanks, man. That solved it and now I feel silly :wink:

Jeff.