【发布时间】:2012-09-10 12:20:09
【问题描述】:
MissingReferenceException:“GameObject”类型的对象已被 已销毁,但您仍在尝试访问它。你的脚本应该 检查它是否为空,或者您不应该销毁该对象。 UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object 数据,Vector3 pos,四元数腐烂)(在 C:/BuildAgent/work/d9c061b1c154f5ae/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:44) UnityEngine.Object.Instantiate(UnityEngine.Object 原版,Vector3 位置,四元数旋转)(在 C:/BuildAgent/work/d9c061b1c154f5ae/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:53) Gun.Update () (在 J:/W.A.R/Assets/Scripts/Gun.cs:16)
这就是我得到的错误,这里是我的枪类代码
using UnityEngine;
using System.Collections;
public class Gun : MonoBehaviour {
public GameObject bullet = null;
public float bulletSpeed = 500f;
void Update () {
if (Input.GetKeyDown (KeyCode.Mouse0)) {
/*
* Instantiate the bullet using the prefab and store it into the shot variable
*/
GameObject shot = GameObject.Instantiate(bullet, transform.position + (transform.forward * 2), transform.rotation) as GameObject;
/*
* Adding force
*/
shot.rigidbody.AddForce(transform.forward * bulletSpeed);
}
}
}
【问题讨论】:
-
看起来您正在尝试实例化一个项目符号,但是,您将项目符号设置为 null,并且在提供的代码中没有将其设置为其他值。您不能使用空对象。
-
GameObject 看起来像一个 Base 类,所以您可能想要一个属于 GameObject 类型的 Bullet 类,因此设置 bullet = new Bullet()。
-
@user1526784 查看我的答案以获取示例。
标签: c# unity3d frame-rate gameobject