title: Taking damage in unity 2D
body: <p>I am making a script where the player triggers with the enemy and takes the damage from him.</p>
  <pre><code>public class PlayersHealth : MonoBehaviour
  {
      public int currentHealth;
      public int maxHealth;
      private bool inCollider = false;
  
      void Start()
      {
          currentHealth = maxHealth;
      }
  
      void OnTriggerEnter2D(Collider2D collision){
       if(inCollider == false){
       if(collision.CompareTag(&quot;Enemy&quot;)){
          inCollider = true;
       }
       }
      }
  
      void OnTriggerExit2D(Collider2D collision){
          if(inCollider == true){
          if(collision.CompareTag(&quot;Enemy&quot;)){
             currentHealth--;
             inCollider = false;
             }
          }
      }
  }
  </code></pre>
  <p>The main problem is that I am taking to much damage because of this going in/out of the trigger collider.</p>
  <p>I have tried to fix that by making bool &quot;inCollider&quot; that should solve the problem.
  Unfortunately, problem still remains.</p>
  <p>I am a newbie to Unity and C#, so I would be glad to see alternative ways to do a script like this.</p>
  <p>Maybe I should do this in enemy attack script?</p>