I’m trying to add a method to an action. It compiles fine, however during run time I’ll get the error listed in the title.
This is the error line:
newObject.GetGameObject().GetComponent<Enemy>().OnDeath += lM.RemoveEnemyFromList;
Edit: I should mention that newObject is not a GameObject(it’'s more a pool object). It contains a GameObject, hence why I have a GetGameObject() function…
// PoolManager script
public void CreatePool(GameObject prefab, int poolSize)
{
// Make the poolKey equal to the ID of the prefab
int poolKey = prefab.GetInstanceID();
// Check to see if the dictionary doesn't already contain the poolkey
if (!poolDictionary.ContainsKey(poolKey))
{
// Create a new Key-Value Pair with key: poolKey and a new Queue
poolDictionary.Add(poolKey, new Queue<PoolObjectInstance>());
for (int i = 0; i < poolSize; i++)
{
// Make a new instance of our prefab
PoolObjectInstance newObject = new PoolObjectInstance(Instantiate(prefab) as GameObject);
if (newObject.GetGameObject().GetComponent<Enemy>())
{
newObject.GetGameObject().GetComponent<Enemy>().OnDeath += lM.RemoveEnemyFromList; // TODO: THIS NEEDS TO CHANGE / HAVING POOL MANAGER INCLUDE LEVEL MANAGER IS NO GOOD.
}
// Now add this instance to our Queue at the poolKey we just specified
poolDictionary[poolKey].Enqueue(newObject);
}
}
}
//Enemy Script
public Action<Enemy> OnDeath;
public void TakeDamage(float damage)
{
health -= damage;
if (health <= 0)
{
OnDeath(this);
gameObject.SetActive(false);
}
}