关于FBX模型导入Unity后,在Play场景里想要选中但报错:“NullReferenceException: Object reference not set to an insta”的解决办法。

FBX导入Unity后,本想要测试通过做一个BIM模型数字孪生场景,实现Play场景下,选择构件能展示构件属性信息。代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Demo1 : MonoBehaviour
{
    public float smooth = 3f;
    Transform currentObject;
    Vector3 mouse3DPosition;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButton(0))//判断鼠标左键是否被单击
        {
            // 创建一条点击位置为光标位置的射线
            Ray rays = Camera.main.ScreenPointToRay(Input.mousePosition);
            //将射线以黄色的表示出来
            Debug.DrawRay(rays.origin, rays.direction * 100, Color.yellow);
            //创建一个RayCast变量用于存储返回信息
            RaycastHit hit;
            //将创建的射线投射出去并将反馈信息存储到hit中
            if(Physics.Raycast(rays,out hit))
            {
                //获取被射线碰到的对象transfrom变量
                currentObject = hit.transform;
            }
            Debug.Log(currentObject.name);
        }
        //如果鼠标左键被抬起,就将涉嫌获取到的对象删除
        if (Input.GetMouseButtonUp(0))
        {
            currentObject = null; 
        }
    }
}

但发现导入FBX后,发现想要选择时,Log提示:“NullReferenceException: Object reference not set to an insta”。发现问题不对之后,清空了场景,新建了一个Cube,能够正常提示。后来发现问题根源是:FBX的模型,要添加上Mesh Collider才可以被射线捕捉到。

关于FBX模型导入Unity后,在Play场景里想要选中但报错:“NullReferenceException: Object reference not set to an insta”的解决办法。_第1张图片

好了!!!问题解决!!!

你可能感兴趣的:(unity,游戏引擎)