![](/img/trans.png)
[英]GameObjects in a public static Dictionary<String,GameObject> are destroyed on scene changes in Unity
[英](Unity) Destroy a gameObject in the current scene and keep it permanently destroyed even after loading current scene?
如何永久销毁gameObject? 也就是说,如果我要在当前场景中销毁游戏对象,那么我只希望它在那之前存在一次,然后在重新加载同一场景时,我希望它被禁用/销毁。 我该如何实现?
编辑:或者下面的脚本只能与布尔变量一起运行一次?
void Update()
{
if(Input.GetKeyDown(KeyCode.A)
{
GameObject go = GameObject.Find("Cloner");
Destroy(go);
SceneManager.LoadScene("Home");
}
}
我通过在函数中使用DontDestroyOnLoad()做到了
无效Awake()
static bool created = false;
public bool Loading = true;
void Awake()
{
if(!created)
{
DontDestroyOnLoad(this.gameObject);
created = true;
}
else
{
Destroy(this.gameObject);
}
}
void Update()
{
if(Input.GetKeyDown(KeyCode.A) && LoadingOnce == true)
{
GameObject go = GameObject.Find("Cloner");
Destroy(go);
LoadingOnce = false;
SceneManager.LoadScene("Home");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.