NullReference Expcetion... But it shouldn't be? C#

NullReferenceException: Object reference not set to an instance of an object
Factory+c__Iterator0.MoveNext () (at Assets/Blank Canvas/Scripts/Managers/Factory/Factory.cs:64)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
Factory:Start() (at Assets/Blank Canvas/Scripts/Managers/Factory/Factory.cs:42)

The weird thing that I can’t understand is the line of code that is giving me the error I’ve used in previous projects. I’ve been back over two script files from older games and the line of code is the same (with the obvious method differences).

I’m using object pooling that I have in a script called ObjectPoolManager which has a public static method called Create. I’ve taken this straight from my other projects as well which I’ve just run. The entire ObjectPoolManager is designed to fit any game and any need so it’s the same across anything I’ve used it in.

The error is on line 42 of my Factory script below:

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

public class Factory : MonoBehaviour {

	#region Singleton
	private static Factory sInstance;
	public static Factory Instance { get { return sInstance; } }

	void Awake()
	{
		if(sInstance == null)
			sInstance = this;
		else
			enabled = false;

		Initialise();
	}
	#endregion

	//General Properties
	private bool				m_EnemySpawned 	= false;
	public float 				m_SpawnRate 	= 1.0f;

	public GameObject[] 		m_EnemyPrefabs;
	private List<Enemy> 		m_SpawnedEnemies;

	//GA Properties
	public bool					m_UseAI 		= false;

	void Initialise()
	{
		m_SpawnedEnemies = new List<Enemy>();
	}

	void Start()
	{
		if(!m_UseAI)
		{
			StartCoroutine(SpawnRandomly());
		}
	}

	void Update()
	{

	}

	IEnumerator SpawnRandomly()
	{
		while(true)
		{
			if(!m_EnemySpawned) // Spawn
			{
				//Get random spawn tile
				//int random = new Random.Range(0, GameManager.SpawnPoints.Length);
				int random = CMath.Random(0, GameManager.SpawnPoints.Length);
				GameObject Tile = GameManager.SpawnPoints[random];
				Debug.Log ("Spawning at Tile: " + random.ToString());

				//Create a random enemy at the spawn tile with an arbitrary rotation
				GameObject 	NewEnemy 	= ObjectPoolManager.Create(RandomEnemy(), Tile.transform.position, Quaternion.identity);
				Enemy		NewEntity 	= NewEnemy.GetComponent<Enemy>();
				m_SpawnedEnemies.Add(NewEntity);

				m_EnemySpawned = true;
			}
			else if(m_EnemySpawned) // Spawn cooldown
			{
				m_EnemySpawned = false;
				yield return new WaitForSeconds(m_SpawnRate);
			}
		}
	}

	//Returns a random GameObject from the enemy prefab array
	private GameObject RandomEnemy()
	{
		int rand = Random.Range(0, m_EnemyPrefabs.Length - 1);
		Debug.Log ("Spawning: " + m_EnemyPrefabs[rand].name);
		return m_EnemyPrefabs[rand];
	}
}

It would make sense for the error to be from having now GameObject newObject = new GameObject(); but I’ve used GameObject newObject = ObjectPoolManager.Create(…); before and it’s never given me a NullReference. I’m also using two debug statements to ensure it’s returning an actual tile to spawn at an and actual enemy to spawn which both output correctly just before the exception.

Any ideas?

I’ve sorted the problem, Tile’s position was actually null due to an error in the prefab I was using. I sorted it by putting in Vector2.zero into my ObjectPoolManager.Create() and it worked. I’ll go sort my tile system now.