With only the given information I would assume one of a few scenarios is happening here:
Something very mysterious because you provided very little information
Your player is on the enemy layer and subsequently the ray immediately hits the player
You are not setting up your EnemyLayer variable correctly.
To filter a Raycast based on layer you need to set up a layer mask. A layer mask is the result of bitshifting (>>) the layer numbers and combine multiple layer masks into a final layer mask using the binary OR operator (|).
int layer1 = 3;
int layer2 = 5;
int layermask1 = 1 << layer1;
int layermask2 = 1 << layer2;
int finalmask = layermask1 | layermask2; //variable to use as the Raycast() layermask parameter
Given your specific code, we are only working with one layer it looks like so your code might look something like this assuming you've set up the enemy layer on the first available custom layer which would be layer 8.
int enemyLayer = 8;
int enemyMask = 1 << enemyLayer;
//int layerMask = enemyMask; //No need for setting up a final mask because we only have one layer to check
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.right, enemyMask);
To comment on your edit:
The above should help you resolve the issue with not needing to disable your player collider. The null reference exception is likely due to the fact that you aren't colliding anything. You need to actually check if hit.collider
is null as hit will return information even if it doesn't hit.