Don't see the cause of this NullReferenceException...

When I try to assign the instantiated object to batReferences[0].enemy in the array, it throws: NullReferenceException: Object reference not set to an instance of an object. Can anyone tell me why? It’s nothing to do with the instantiated object not existing, I can try assigning any gameObject to the array and it throws the same error…

var batPrefab : GameObject;
private var batReferences : EnemyClass[];

class EnemyClass
{
	var enemy : GameObject;
	var material : Material;
	var batScript : BatScript;
	var activated : boolean;
}

function Start ()
{
	// Instantiate enemy
	batReferences = new EnemyClass[3];
	var newEnemy = Instantiate( batPrefab );
	batReferences[0].enemy = newEnemy;    // NullReferenceException
}

You’ve created an empty array, but you’ve not created and initialized all the EnemyClass instances for the elements in the array. All you have is an array of null references. You need to add some array initialization:

for (var i = 0; i < batReferences.Length; i++) 
    batReferences *= new EnemyClass();*