ArgumentNullException when instantiating prefabs using generic list

Hi, I’m lost with this. I’m sure I did everything right but I get an ‘ArgumentNullException: Argument cannot be null.’-error when I start the game.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Portal : MonoBehaviour {

    List<GameObject> femalePrefabs;
    List<GameObject> malePrefabs;

    int femalePrefabIndex = 0;
    int malePrefabIndex = 0;

    GameObject agentToSpawn;

    public int totalAgentsToSpawn = 20;

    public float spawnInterval = 0.5f;

    public enum Gender
    {
        Female, Male, RandomlyMixed, EvenlyMixed
    }
    public Gender genderToSpawn = Gender.EvenlyMixed;

    bool gate = true;

	// Use this for initialization
	void Start()
    {
     /*///(ArgumentNullException ->)///*/  femalePrefabs = new List<GameObject>(Resources.LoadAll("Characters/_Prefabs/Low Poly/Female") as GameObject[]);
        malePrefabs = new List<GameObject>(Resources.LoadAll("Characters/_Prefabs/Low Poly/Male") as GameObject[]);

        StartCoroutine(SpawnAgents());
	}

	public IEnumerator SpawnAgents()
    {
        if(totalAgentsToSpawn > 0)
        {
            switch(genderToSpawn)
            {
                case Gender.Female:
                    if(femalePrefabIndex < femalePrefabs.Count)
                    {
                        agentToSpawn = Instantiate(femalePrefabs[femalePrefabIndex], transform.position, Quaternion.identity) as GameObject; 
                        femalePrefabIndex++;
                    }
                    else
                    {
                        agentToSpawn = Instantiate(femalePrefabs[Random.Range(0, femalePrefabs.Count)], transform.position, Quaternion.identity) as GameObject; 
                    }
                    break;

                case Gender.Male:
                    if(malePrefabIndex < malePrefabs.Count)
	                {
		                agentToSpawn = Instantiate(malePrefabs[malePrefabIndex], transform.position, Quaternion.identity) as GameObject; 
	                    malePrefabIndex++;
                    }
                    else
                    {
                        agentToSpawn = Instantiate(malePrefabs[Random.Range(0, malePrefabs.Count)], transform.position, Quaternion.identity) as GameObject; 
                    }
                    break;
                case Gender.EvenlyMixed:
                    if(gate)
                    {
                        gate = false;
                        goto case Gender.Female;
                    }
                    else
                    {
                        gate = true;
                        goto case Gender.Male;
                    }
                case Gender.RandomlyMixed: bool b = Get.RandomBool();
                    if(b)
                        goto case Gender.Female;
                    else
                        goto case Gender.Male;
            }

            totalAgentsToSpawn--;

            //stuff that has to do with 'agentToSpawn'

            yield return new WaitForSeconds(spawnInterval);

            StartCoroutine(SpawnAgents()); 
        }
    }
}

The paths to the prefabs are valid. What am I doing wrong?

You said you’re getting an ArgumentNullException on this line…

femalePrefabs = new List(Resources.LoadAll("Characters/_Prefabs/Low Poly/Female") as GameObject[]);

I see 2 calls here that accept arguments. Resources.LoadAll accepts a string parameter which clearly isn’t null. Your generic list constructor accepts an IEnumerable argument. So I think that’s our culprit.

If I had to guess at this point, I would say that something returned from the LoadAll call is not a game object (perhaps its a texture2d or material?) and as a result the cast is silently failing.

Suggest breaking that line into it’s components…

UnityEngine.Object[] objs = Resources.LoadAll("Characters/_Prefabs/Low Poly/Female");
// check objs

IEnumerable<GameObject> gos = objs.Cast<GameObject>();
GameObject[] temp = gos.ToArray();
// the ToArray() call forces the casts to take place (see deferred execution)
// if you get an InvalidCastException then my guess above was right
// check temp

femalePrefabs = new List(gos)

hope this helps.

Of course once you’ve found the issue you can condense this back down by changing this:

femalePrefabs = new List(Resources.LoadAll("Characters/_Prefabs/Low Poly/Female") as GameObject[]);

to this…

femalePrefabs = new List(Resources.LoadAll("Characters/_Prefabs/Low Poly/Female").Cast<GameObject>());