- Home /
Question by
SuperMasterBlasterLaser · Feb 12, 2015 at 06:53 AM ·
c#arraynullreferenceexceptionconvertarraylist
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.
Comment
Best Answer
Answer by NoseKills · Feb 12, 2015 at 07:12 AM
Try
basisScript.enemies = (GameObject[])enemyVehicles.ToArray(typeof(GameObject));
Your answer
Follow this Question
Related Questions
NullReferenceException: Array C# 1 Answer
Why is passing this array causing some of its values to become null? 2 Answers
String Parsing 1 Answer
Add to Array 1 Answer