重新加载场景会导致脚本停止工作 [英] Reloading scene causes scripts to stop working

查看:318
本文介绍了重新加载场景会导致脚本停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的脚本可以重新加载场景.该功能由按钮OnClick()事件调用.

I have a simple script that reloads my scene. The function is called by a button OnClick() event.

public void ReloadScene(){
    SceneManager.LoadScene (SceneManager.GetActiveScene ().name);
}

此功能应重新启动当前场景.它确实达到了我的预期,但是有一个问题:大多数脚本无法正常工作.

This function should restart the current scene. It does what I expected it to, but with one problem: most scripts don't work.

唯一有效的脚本是音频源,以及我编写的另一个简单脚本.我只要按一下播放"就不会出错.

The only scripts that work are the Audio Source, and another simple script that I have written. I get no errors when I just press "play".

我正在使用Unity 5.6.0b9 Personal.

I am using Unity 5.6.0b9 Personal.

推荐答案

重新加载场景并不会重置变量,除非您正在使用static变量.一个对象的新实例会将每个对象重置为其初始状态.静态变量不被破坏,因为它们不属于实例.您可以手动重置它们.

Reloading a scene does reset the variables unless you are using static variables. A new instance of an object would reset each object to their initial state. Static variables are not destroyed as they do not belong to an instance. You can reset those manually.

DontDestroyOnLoad()有点不同.它要求Unity在不破坏新场景的情况下不要破坏对象.因此,这些对象不会被销毁(重置).

DontDestroyOnLoad() is a little different. It asks Unity not to destroy an object if a new scene is laoded. So these objects won't get destroyed (reset).

如果您已经动态添加了一些脚本,那么它们也将被销毁.除非将它们添加到DontDestroyOnLoad()父级.

If you have dynamically added some scripts, then they will get destroyed as well. Unless, they are added to a DontDestroyOnLoad() parent.

这篇关于重新加载场景会导致脚本停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆