Hi guys,
I have two teams which are using a base entity script for all units. These entities are added to an entity/spawn manager which has a dictionary:
public Dictionary<Team, List> entitiesByTeam = new Dictionary<Team, List>();
The issue I am having is when the entity is destroyed upon death of the unit it will throw a missing reference exception error. Upon death of the unit I have the following code :
public virtual void Die()
{
Debug.Log(transform.name + "died.");
entityManager.RemoveEntity(entity);
}
To remove it from the dictionary and entity manager:
public void RemoveEntity(BaseEntity entity)
{
entitiesByTeam[entity.myTeam].Remove(entity);
allEntities.Remove(entity);
Destroy(entity.gameObject);
}
Some more methods for reference:
public List<BaseEntity> GetEntitiesAgainst(Team enemyTeam)
{
switch (enemyTeam)
{
case Team.blueTeam:
return entitiesByTeam[Team.redTeam];
case Team.redTeam:
return entitiesByTeam[Team.blueTeam];
/*case neutral: // Setup neutral jungle units here
break;*/
default:
return entitiesByTeam[Team.neutral];
}
}
protected void FindTarget()
{
var allEnemies = EntityManager.instance.GetEntitiesAgainst(myTeam); //One team is not finding the currentTarget from the other team. Need to fix.
float minDistance = Mathf.Infinity;
BaseEntity candidateTarget = null;
foreach (BaseEntity e in allEnemies)
{
if (candidateTarget == null)
{
candidateTarget = e;
//Debug.Log("Assigning any knight as closest.");
}
else
{
if (Vector3.Distance(e.transform.position, currentPosition.position) <=minDistance)
{
//Debug.Log("Finding the closest enemy.");
minDistance = Vector3.Distance(e.transform.position, currentPosition.position);
candidateTarget = e;
}
}
}
currentTarget = candidateTarget;
}
Honestly thought this code was working. I have gone through and followed a youtube tutorial that seemed to have worked. Maybe I missed something. Please let me know if you can spot anything obvious.
Cheers,
Lettus