title: C# dictionary remove failed
body: <p>This is in Unity.
  When I try to remove an element I've added to this particular dictionnary, the remove fails and i dont understand the behaviour.</p>
  <p>Here is the declaration :</p>
  <pre><code>private Dictionary&lt;int, EnemyControler&gt; remainingMobs = new Dictionary&lt;int, EnemyControler&gt;();
  </code></pre>
  <p>At some point, I do that :</p>
  <pre><code>remainingMobs.Add(newEnemy.MobID, newEnemy);
  </code></pre>
  <p>MobID being an int I increment every time I create a new enemy, which is attached to each of them</p>
  <p>When the enemy dies, in his Destroy() method, he calls this :</p>
  <pre><code>public void EnemyDied(int someID) {
      if(remainingMobs.Remove(someID)) {
          print(&quot;removed &quot;+someID);
      } else {
          print(&quot;Failed to remove &quot; + someID);
          DisplayRemainingMobs();
      }
  }
  </code></pre>
  <p>This is the DisplayRemainingMobs method :</p>
  <pre><code>public void DisplayRemainingMobs() {
      string availableKeys=&quot;&quot;;
      foreach (KeyValuePair&lt;int, EnemyControler&gt; pair in remainingMobs){
          availableKeys+= pair.Key + &quot;, &quot;;
      }
      print(remainingMobs.Keys.Count + &quot; are available : &quot; + availableKeys);
  }
  </code></pre>
  <p>This is where things go wrong.
  As I call the DisplayRemainingMobs() earlier, the output is :</p>
  <blockquote>
  <p>2 are available : 1, 2,</p>
  </blockquote>
  <p>Which is completely what is expected.
  But then, as I kill one of the two that I've spawned, and the .remove() is called on the dictionary, the output of the print becomes :</p>
  <blockquote>
  <p>Failed to remove 2</p>
  <p>0 are available :</p>
  </blockquote>
  <p>I don't understand why the .remove() fails, nor why after it failed no more keys are available. I have tried a lot of things, including swapping the int to string thinking it may came from there. I can't get to debug this, and whatever I print only tells me it should work. The numbers match during my tests.</p>
  <p>I have even made a full function where I use all those function on another dictionary specially made for this, to be sure, and everything goes as it should.</p>
  <p>I have in mind that it may come from the fact that the remove is called from a method, but then how can I solve that, please ?</p>
  <p>(cant tag dictionary apparently because of low reput)</p>
  <p>Edit : here is the complete code :
  This is the WaveControler class (the one with the most code in it)</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class WaveControler : MonoBehaviour {
  
  public EnemyControler enemy;
  public Transform spawnLocation;
  private EnemyControler newEnemy;
  private int waveNumber;
  private Dictionary&lt;int, EnemyControler&gt; remainingMobs = new Dictionary&lt;int, EnemyControler&gt;();
  private int mobID;
  
  void Start () {
      waveNumber =2;
      mobID=0;
      Spawn();
  }
  
  void Update () {        
      if(AllEnemiesDead()) {
          print(&quot;GO TO NEXT WAVE&quot;);
          GoToNextWave();
          Spawn(); 
      } else {
          DisplayRemainingMobs();
      }
  }
  
  void  Spawn() {
      for(int i=0; i&lt;waveNumber; i++) {
          print(&quot;Wave #&quot;+waveNumber+&quot; debug : i=&quot;+i);
          newEnemy = Instantiate(enemy, spawnLocation.position, spawnLocation.rotation) as EnemyControler;
          newEnemy.MobID= ++mobID;
          print(&quot;Spawning mob #&quot; +mobID.ToString());
          remainingMobs.Add(newEnemy.MobID, newEnemy);
      }
  }
  
  public void GoToNextWave() {
      print(&quot;Wave &quot;+waveNumber+&quot; completed.&quot;);
      waveNumber++;
  }
  
  public void EnemyDied(int someID) {
      if(remainingMobs.Remove(someID)) {
          print(&quot;removed &quot;+someID.ToString());
      } else {
          print(&quot;Failed to remove &quot; + someID.ToString());
          DisplayRemainingMobs();
  
      }
  }
  
  public bool AllEnemiesDead() {
      print(&quot;remainingMobs.Count==0 ? &quot; + (remainingMobs.Count==0));
      return remainingMobs.Count==0;
  }
  
  public void DisplayRemainingMobs() {
      string availableKeys=&quot;&quot;;
      foreach (KeyValuePair&lt;int, EnemyControler&gt; pair in remainingMobs){
          availableKeys+= pair.Key + &quot;, &quot;;
      }
      print(remainingMobs.Keys.Count + &quot; are available : &quot; + availableKeys);
  }
  </code></pre>
  <p>}</p>
  <p>And this is the class with the .Destroy()</p>
  <pre><code>using System;
  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class EnemyControler : MonoBehaviour {
  
  private Rigidbody myRB;
  public WaveControler wave;
  public float moveSpeed;
  
  public PlayerControler thePlayer;
  
  public int MobID { get; set; }
  
  // Use this for initialization
  void Start () {
      myRB = GetComponent&lt;Rigidbody&gt;();
      thePlayer = FindObjectOfType&lt;PlayerControler&gt;();
  }
  
  // Update is called once per frame
  void Update () {
      transform.LookAt(thePlayer.transform.position);
  }
  
  private void FixedUpdate() {
      myRB.velocity = (transform.forward *moveSpeed);
  }
  
  private void OnDestroy() {
      wave.EnemyDied(MobID);
  }
  
  public override string ToString(){
      return String.Format(&quot;i'm number &quot;+ MobID);
  }
  </code></pre>
  <p>}</p>