我在学习【超详细Unity3D官方案例【沙漠坦克大战】游戏完整教程~初中级学习者-哔哩哔哩】 https://b23.tv/ohzCnJH
第#4中,5:51,视频内第21行代码。
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
GameObject go = GameObject.Instantiate(shellPrefab, firePoint.position, firePoint.rotation) as GameObject;
go.GetComponent().velocity = go.transform.forward * shellSpeed;
}
}
视频里完美运行,而我在按空格(即Fire1)时,我的unity控制台却报错
“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.”
Unity控制台报错指向“GameObject go = GameObject.Instantiate(shellPrefab, firePoint.position, firePoint.rotation) as GameObject;”这一行代码。
明明还没有输入destroy相关代码,却报这个错,实在穷途末路了。
类似问题也有在其他游戏脚本里发生过,但是实在不知道怎么解决,都是鸽着的,这已经是我第三次遇到这个报错了,珊珊迟来这里求问
希望按下空格能正常发射出炮弹
shellPrefab
是在Inspector
面板上拖拽赋值的,去你的Hierarchy
面板找到,挂载这个脚本的物体,将子弹的预制体拖拽到上面赋值即可
shellPrefab是个什么东西,炮弹的实体吗
你得确保它一直存在,可以把它隐藏在地下等看不见的地方
千万别销毁了
你源物体都销毁了,那生成肯定报错啊
看上去是ShellPrefab已经是null了,但是你还在通过它实例化创建物体。