KeyNotFoundException: The given key was not present in the dictionary.

I have been getting the above error on infrequent and irregular occasions whenever a certain part of a spawning script in my game is run, when the enemies are spawned. The enemies spawn in waves and I have gotten this error on different waves. It rarely occurs, but it has happened enough times that I know there is a problem. My real issue is that, I don’t know what the error means and I don’t really understand the part of the script it says is giving me the error, and I can’t really figure it out because it does not occur consistently. My spawning system works perfectly almost all of the time except when I get this weird error.

This is the script:

	private SpawnManager manager;
	Transform theManager;
	// Color of the gizmo
	public Color gizmoColor =;
	// Spawn types
	public enum SpawnTypes
	// The different Enemy levels
	public enum EnemyLevels
	// Enemy level to be spawnedEnemy
	public EnemyLevels enemyLevel = EnemyLevels.Easy;

	public GameObject EasyEnemy;
	public GameObject MediumEnemy;
	public GameObject HardEnemy;
	public GameObject BossEnemy;
	private Dictionary<EnemyLevels, GameObject> Enemies = new Dictionary<EnemyLevels, GameObject>(4);
	// End of Enemy Prefabs
	// Enemies and how many have been created and how many are to be created
	public int totalEnemy = 10;
	public int numEnemy = 0;
	private int spawnedEnemy = 0; 
	// End of Enemy Settings
 	Transform thetarget;
	// The ID of the spawner
	private int SpawnID;
	//Timer between waves
	public float thetimer = 15.0f;

	// Different Spawn states and ways of doing them
	private bool waveSpawn = false;
	public bool Spawn = true;
	public SpawnTypes spawnType = SpawnTypes.Normal;
	public int totalWaves = 5;
	public int numWaves = 0;
	public bool newWave = false;
	public bool scriptsReady = false;
	public bool SpawnTwo = false;

	private float spawndelay = 5.0f;
	Transform headsupdisplay;

	void Start()
		if (thetarget == null && GameObject.FindWithTag("Player")){
			thetarget = GameObject.FindWithTag("Player").transform;
		if (headsupdisplay == null && GameObject.FindWithTag("HeadsUpDisplay")){
			headsupdisplay = GameObject.FindWithTag("HeadsUpDisplay").transform;
		if (theManager == null && GameObject.FindWithTag("SpawnManage")){
			theManager = GameObject.FindWithTag("SpawnManage").transform;
		// sets a random number for the id of the spawner
		SpawnID = Random.Range(1, 500);
		Enemies.Add(EnemyLevels.Easy, EasyEnemy);
		Enemies.Add(EnemyLevels.Boss, BossEnemy);
		Enemies.Add(EnemyLevels.Medium, MediumEnemy);
		Enemies.Add(EnemyLevels.Hard, HardEnemy);
		manager = GameObject.FindWithTag("SpawnManage").GetComponent<SpawnManager>();	


	// Draws a cube to show where the spawn point is... 
	void OnDrawGizmos()
		// Sets the color to red
		Gizmos.color = gizmoColor;
		//draws a small cube at the location of the game object that the script is attached to
		Gizmos.DrawCube(transform.position, new Vector3 (0.5f,0.5f,0.5f));
	void Update ()
		SpawnTwo = manager.readytospawn;
			// Spawns enemies everytime one dies
			if (spawnType == SpawnTypes.Normal)
				// checks to see if the number of spawned enemies is less than the max num of enemies
				if(numEnemy < totalEnemy)
					// spawns an enemy
			//spawns enemies in waves, so once all are dead, spawns more
			else if (spawnType == SpawnTypes.Wave)
	// spawns an enemy based on the enemy level that you selected
	private void spawnEnemy()
		if (spawndelay>=0){
			spawndelay -= Time.deltaTime;
		if (spawndelay<=0){

//This is the part of the code that gives me an error

			GameObject Enemy = (GameObject) Instantiate(Enemies[enemyLevel], gameObject.transform.position, Quaternion.identity);

//end of error causing code
			Enemy.SendMessage("setName", SpawnID);
			// Increase the total number of enemies spawned and the number of spawned enemies
			spawndelay = 10.0f;
	// Call this function from the enemy when it "dies" to remove an enemy count
	public void killEnemy(int sID)
		// if the enemy's spawnId is equal to this spawnersID then remove an enemy count
		if (SpawnID == sID)

I realize that this code has some unused variables and so on, I haven’t really cleaned it up yet, but that shouldn’t affect the part of the script that is giving me trouble. If anyone could explain what the error is, or means, and hopefully a way to fix it I would be most grateful.

Line 164: Enemies[enemyLevel] - it says that it doesn’t contain an entry for enemyLevel.

Looking through your code, this should only happen if you had an exception happen in Start, before you add enemies on line 89.

Another way it could happen is if any code sets enemyLevel to something else outside the script, as when casting from an integer to an enum and supplying an incorrect value.