title: C#/Unity: NPCs Skipping Lines of Dialogue
body: <p>I'm trying to use a Dialogue Manager to handle NPC speech in my game, and I'm running into some trouble.</p>
  
  <p>I have two NPC's; if I only talk to one, it'll go through all of their lines, no problem (each NPC has five lines of code, triggered by the PC going up to them and pressing Space). However, if I talk to NPC1, then NPC2, it will only ever display NPC2's speech, even if I go back to NPC1.</p>
  
  <p>NPC.cs</p>
  
  <pre><code>public class NPC : Character {
      private bool charInRange;
      public Dialogue dialogue;
      public bool talkedTo = false;
  
      // Use this for initialization
      void Start () {
          charInRange = false;
      }
  
      // Update is called once per frame
      void Update () {
          //if player is in range and presses space, triggers NPC dialogue
          if (charInRange &amp;&amp; Input.GetKeyDown(KeyCode.Space))
          {
              TriggerDialogue();
          }
      }
  
      //if Player gameObject is in NPC collider, player is in range
      void OnTriggerEnter2D(Collider2D other)
      {
          if(other.gameObject.tag == "Player")
          {
              charInRange = true;
          }
      }
  
      //if player exits NPC collider, player is not in range
      void OnTriggerExit2D(Collider2D other)
      {
          if (other.gameObject.tag == "Player")
          {
              charInRange = false;
          }
      }
  
      //if NPC has been talked to before, displays next sentence; if not, loads dialogue and displays first sentence
      private void TriggerDialogue()
      {
          if (!talkedTo)
          {
              talkedTo = true;
              FindObjectOfType&lt;DialogueManager&gt;().StartDialogue(dialogue);
          }
          else
          {
              FindObjectOfType&lt;DialogueManager&gt;().DisplayNextSentence();
          }
      }
  }
  </code></pre>
  
  <p>DialogueManager.cs</p>
  
  <pre><code>public class DialogueManager : MonoBehaviour {
  
  private Queue&lt;string&gt; sentences;
  
  // Use this for initialization
  void Start () {
  
  }
  
  // Update is called once per frame
  void Update () {
  
  }
  
  //loads a queue with lines from Dialogue and displays first sentence
  public void StartDialogue(Dialogue dialogue)
  {
      sentences = new Queue&lt;string&gt;();
  
      foreach (string sentence in dialogue.sentences)
      {
          sentences.Enqueue(sentence);
      }
  
      DisplayNextSentence();
  }
  
  //displays next sentence in the queue
  public void DisplayNextSentence()
  {
      Debug.Log(sentences.Count);
  
      //if no more sentences in the queue, end the dialogue
      if(sentences.Count == 0)
      {
          EndDialogue();
          return;
      }
  
      string sentence = sentences.Dequeue();
      Debug.Log(sentence);
  }
  
  //ends dialogue
  private void EndDialogue()
  {
      Debug.Log("CONVERSATION OVER");
  }
  }
  </code></pre>
  
  <p>I know what the issue is: once NPC2 is talked to, talkedTo in NPC.cs flips, NPC2's dialogue starts, and NPC1's dialogue queue is wiped out. Then, it simply displays NPC2's next sentence, regardless of which collider the player is in. My problem is, I'm not sure how to resolve this. Should I be storing specific dialogue in specific NPC's? If so, how do I do that?</p>
  
  <p>Any and all help is appreciated. Thanks!</p>