c# - MissingReferenceException:“GameObject”类型的对象已被销毁,但您仍在尝试访问它
问题描述
我是团结的初学者,我试图制作简单的球类游戏,其中我生成随机物体,玩家必须保存球以免击中物体,我也在球通过这些物体后摧毁物体,但我得到了这个错误。
MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, UnityEngine.Vector3 pos, UnityEngine.Quaternion rot) (at <9baebf9af86541678fd15bfdbf5f26eb>:0)
UnityEngine.Object.Instantiate (UnityEngine.Object original, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) (at <9baebf9af86541678fd15bfdbf5f26eb>:0)
UnityEngine.Object.Instantiate[T] (T original, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) (at <9baebf9af86541678fd15bfdbf5f26eb>:0)
ObstacleSpawner.spawner () (at Assets/scripts/ObstacleSpawner.cs:26)
myObstacleSpawner 脚本是
public class ObstacleSpawner : MonoBehaviour
{
public GameObject[] obstacle;
// Start is called before the first frame update
void Start()
{
InvokeRepeating("spawner", 0.5f, 0.5f);
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector3.forward * 50 * Time.deltaTime);
}
public void spawner()
{
Instantiate(obstacle[Random.Range(0, 2)], new Vector3(Random.Range(-3f,3f), transform.position.y, transform.position.z), Quaternion.identity);
}
}
我的毁灭者脚本是
public class Destroyer : MonoBehaviour
{
// Start is called before the first frame update
public float speedDestroyer = 10f;
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "enemy")
{
Destroy(other.gameObject);
}
}
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector3.forward * speedDestroyer * Time.deltaTime);
}
}
解决方案
推荐阅读
- mysql - 我的 SQL 查询没有返回预期的结果
- android - 我的警报管理器在应用程序关闭时不显示通知?
- sockets - 收到此错误 SocketException: OS Error: Connection timed out, errno = 110, address = 10.xxx.xx.xxx, port = 43620 in flutter
- c# - 构建错误 - 安装 Nuget 包后访问被拒绝
- jquery - 选择两个选择,通过 jquery 和 json 选择
- amazon-web-services - SAM 节点 + Python lambdas 一起
- html - 如果变量未定义,则在 html 中进行角度检查
- java - 如何使用 jetty-maven-plugin 10 指定 JAAS 配置文件
- css - Wave header with svg - 如何让它响应?
- python - 在 tkinter 中滚动笔记本选项卡