对象调用

时间:2016-09-21 15:27:24

标签: c# unity3d nullreferenceexception

我是新手,所以请耐心等待。我试图通过隐藏由玩家角色发出的linecast数组未触及的相机图块和实体来创建视线效果。我得到了NullReferenceException,似乎在告诉我' hit'没有设置为一个对象,虽然这是真的(根据我的知识,它是一个RaycastHit变量),错误指向的代码行应该用&#命中的gameObject运行。 39;打&#39 ;.有问题的行如下:

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

以下是代码的其余部分:

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;
                }
            }
        }
    }
}

0 个答案:

没有答案