Skip to content

Commit

Permalink
Fixed EnemyManager exception when restarting game
Browse files Browse the repository at this point in the history
E.g. after the base has been destroyed, which automatically restarts the game.
  • Loading branch information
ddabble committed Nov 23, 2021
1 parent 62b6122 commit a81277e
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions Assets/Scripts/Enemy/EnemyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class EnemyManager : MonoBehaviour
private static EnemyManager SINGLETON;

public static readonly Type[] ENEMY_TYPES = { typeof(SkeletonController) };
private static readonly Dictionary<Type, GameObject> ENEMY_TYPE_TO_PREFAB = new Dictionary<Type, GameObject>(ENEMY_TYPES.Length);
private Dictionary<Type, GameObject> enemyTypeToPrefab;

public GameObject[] enemyPrefabs;

Expand All @@ -34,13 +34,14 @@ void Awake()

#endregion Singleton boilerplate

enemyTypeToPrefab = new Dictionary<Type, GameObject>(ENEMY_TYPES.Length);
foreach (GameObject prefab in enemyPrefabs)
{
Type enemyType = prefab.GetComponent<Enemy>().GetType();
if (ENEMY_TYPE_TO_PREFAB.ContainsKey(enemyType))
if (enemyTypeToPrefab.ContainsKey(enemyType))
throw new ArgumentException($"Enemy type {enemyType} appears more than once among the enemy prefabs!");

ENEMY_TYPE_TO_PREFAB.Add(enemyType, prefab);
enemyTypeToPrefab.Add(enemyType, prefab);
}
}

Expand All @@ -58,6 +59,6 @@ private Vector3 ChoosePosForSpawningEnemy()

public static GameObject GetEnemyPrefab(Type enemyType)
{
return ENEMY_TYPE_TO_PREFAB[enemyType];
return SINGLETON.enemyTypeToPrefab[enemyType];
}
}

0 comments on commit a81277e

Please sign in to comment.