Collision2D Calculations

Hi!
I’ve got a little problem with Calculating a Collisions.I want my Game to calculate the difference in positions betwen Player and Every GameObjects with NPCEssentialsScript.The Player is for each A and D key moving 6.4 in the X axis and 10 for each W and S Key in Y axis.

public class PlayerEssentials : MonoBehaviour {
        private bool collW;
    private bool collS;
    private bool collA;
    private bool collD;
      //  ...
       void OnCollisionEnter2D(Collision2D coll)
            if(transform.position.y - NPCEssentials.tr.position.y == -10 && transform.position.x == NPCEssentials.tr.position.x) //W
            {
                print ("W");
                collW = true;
            }
            else if(transform.position.y - NPCEssentials.tr.position.y == 10 && transform.position.x == NPCEssentials.tr.position.x)//S
            {
                print ("S");
                collS = true;
            }
            else if(transform.position.x - NPCEssentials.tr.position.x == 6.4 && transform.position.y == NPCEssentials.tr.position.y)//A
            {
                print ("A");
                collA = true;
            }
            else if(transform.position.x - NPCEssentials.tr.position.x == -6.4 && transform.position.y == NPCEssentials.tr.position.y)//D
            {
                print ("D");
                collD = true;
            }
        }
    }
    void OnCollisionExit2D()
    {
        collW = false;
        collS = false;
        collA = false;
        collD = false;
    }
}

and the NPCEssentials.tr = transform(of the NPC).
When I’m running my game I’m getting NullReferenceException for each If in the code, but everyting is declared.

you’re not declaring the link of NPCEssentials anywhere in the above code. Something like:

public NPCEssentials _npce
_npce = this.GetComponent<NPCEssentials>();

_npce.tr.position.y etc etc

Nothing has changed.
I want to set in that if that the every GameObject with NPCEssentials script will Collide with my Player.
I really don’t know what the Console wants to tell me because everything is initialized.
I’ve dragged the Player in the Unity to the Player property in the inspector.

I’ve modified a part of my Script
Here it is:

if(coll.gameObject.tag == "NPC" || coll.gameObject.tag == "Object")
        {
            GameObject _NPC = coll.gameObject;
      
          
            if(Player.position.y - _NPC.transform.position.y == -10 && Player.position.x == _NPC.transform.position.x) //W
            {
                print ("W");
                collW = true;
            }
            else if(Player.position.y - _NPC.transform.position.y == 10 && Player.position.x == _NPC.transform.position.x)//S
            {
                print ("S");
                collS = true;
            }
            else if(Player.position.x - _NPC.transform.position.x == 6.4 && Player.position.y == _NPC.transform.position.y)//A
            {
                print ("A");
                collA = true;
            }
            else if(Player.position.x - _NPC.transform.position.x == -6.4 && Player.position.y == _NPC.transform.position.y)//D
            {
                print ("D");
                collD = true;
            }
        }

But when I’m tryin to make a collision,I can’t see my Custom Console Messages inside of the IF’s.
What am I doing wrong ?

I was also tryin to replase in the IF’s “_NPC” to “coll” but still nothing happens.
Everything is good :frowning: .