Arraylist to GameObject[] array issue.

Hello.

I have ArrayList that stores GameObjects as Object class elements. I want to make it GameObject[] array and set it to public attribute:

BasisScript basisScript = myVehicle.GetComponent<BasisScript>();

basisScript.enemies =  enemyVehicles.ToArray() as GameObject[];

But when my BasisScript tries to access to elements of its array, it thows me NullReferenceException. All my components, arraylists and scripts are generated by code.

Question: Why this exception happens? I tried to check if my ArrayList by using Debug.Log() to count my elements and its shows me its alright.

Try

basisScript.enemies = (GameObject[])enemyVehicles.ToArray(typeof(GameObject));