当我尝试使用Raycast
添加到我的广告资源时,游戏会运行!脚本有效!我认为问题是将物品存放到库存中?
有人知道如何解决这个问题吗?先感谢您。
我不知道添加它的正确方法。 :(
public Inventory inventory;
RaycastHit hit;
GameObject pickedUpObject;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.Escape)) {
Screen.lockCursor = false;
}
else {
Screen.lockCursor = true;
}
Ray rayc = Camera.main.ViewportPointToRay (new Vector3 (0.5f, 0.5f, 0f));;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast (rayc.origin, rayc.direction,out hit, 1.5f)) {
if(Input.GetKeyDown("e")){
if(hit.collider.gameObject.tag=="GameItem"){
pickedUpObject=hit.collider.gameObject;
Destroy(pickedUpObject);
inventory.AddItem(pickedUpObject.GetComponent<Item>());
}
}
}
}
然后我尝试了对撞机
enter code here
它正在工作!但我想要Raycast
! :(
&#34; NullReferenceException:未将对象引用设置为对象的实例 Raycasting.Update()(在Assets / Standard Assets / Items / Script / Raycasting.cs:45)&#34;