NullReferenceException when using raycast

hey simply I’m trying to get an enemy to spot the player using ray casting.
I’m getting the error “NullReferenceException: Object reference not set to an instance of an object”
from this line of code

var rayDirection : Vector3 = player.transform.localPosition - transform.localPosition;

Here’s the main chunk of code

var maxDistance : float;
var player : GameObject;

var rayDirection : Vector3 = player.transform.localPosition - transform.localPosition;

var enemyDirection : Vector3 = transform.TransformDirection(Vector3.forward);
var angleDot = Vector3.Dot(rayDirection, enemyDirection);
var playerInFront = angleDot > 0.0;
var playerClose =  rayDirection.sqrMagnitude < maxDistanceSquared;

I admit I am not a out and out programmer but I do usually work things out eventually, has me particularly confused though. Its probably something really obvious hahah

thanks for the help.

Eihter player and/or transform is not set.
If you can’t tell from your code which one it may be, you can do a simple check to see which one that isnt valid:

 if(player == null)
 {
   Debug.Log("player is null");
 }
 if(transform== null)
 {
   Debug.Log("transform is null");
 }

Good luck!