0

I am new to this, so please bear with me. I am trying to create a line-of-sight effect by hiding from the camera tiles and entities not touched by the array of linecast sent out by the players character. I am getting a NullReferenceException that seems to be telling me 'hit' is not set to an object, which while true (it's a RaycastHit variable, to my knowledge), the line of code the error points to is supposed to run off the gameObject of the thing hit by 'hit'. The line in question is below:

hit.transform.gameObject.layer = LayerMask.NameToLayer("Visible");

Here is the rest of the code:

public class LoS : MonoBehaviour 
{
    private Vector3 direction;
    private Vector3 rot1;
    private Vector3 rot2;
    private int d;
    private int r;
    private Ray ray;
    private GameObject hit;

    public Rigidbody point;

    void Start () 
    {
        direction = new Vector3(135, 0, 0);
        rot2 = new Vector3(0, 6, 0);
        rot1 = new Vector3(15, 0, 0);
    }

    void FixedUpdate () 
    {
        d = 0;
        r = 0;
        while (d <= 360) 
        {
            d += 1;
            Ray ray = new Ray(transform.position, direction);
            RaycastHit hit = new RaycastHit();
            Physics.Linecast(transform.position, direction, out hit);
            hit.transform.gameObject.layer = LayerMask.NameToLayer("Visible");
            if (r <= 60) 
            {
                direction += rot2;
                r += 1;
                if (r == 60) 
                {
                    direction += rot1;
                }
            }
        }
    }
}
Nissa
  • 4,636
  • 8
  • 29
  • 37

0 Answers0