[英]Can't Restart Scene in unity 3D
In my game I have several levels, each one has 6 scenes, the scenes names are: Scene 1, Scene 2, Scene 3 ...etc... 在我的游戏中,我有多个级别,每个级别都有6个场景,场景名称为:场景1,场景2,场景3 ...等等...
When the player lose, the current scene should be restarted, so I used this code: 当玩家失败时,应重新启动当前场景,因此我使用了以下代码:
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
I didn't use "(...)GetActiveScene().name);" 我没有使用“(...)GetActiveScene()。name);” because the scenes names are repeated on each level.
因为场景名称在每个级别上都重复出现。
The problem I have is when the player lose in level2 (scene 6), the scene 6 of level1 is loaded instead of restarting scene 6 of level2. 我的问题是,当玩家在级别2(场景6)中失败时,将加载级别1的场景6,而不是重新启动级别2的场景6。 Do you know what's the problem in my code?
您知道我的代码有什么问题吗?
Maybe not an answer to why this happens in your specific case but two suggestions I would prefer: 也许不能回答为什么会在您的特定情况下发生,但是我希望有两个建议:
Just a workaround but how about naming your scenes properly instead like eg Level1_Scene1
, Level2_Scene3
etc 只是一种解决方法,但是如何正确命名场景,例如
Level1_Scene1
, Level2_Scene3
等
so they are all uniquely identifiable using GetActiveScene().name
因此它们都可以使用
GetActiveScene().name
唯一标识
Alternatively you could also try using GetActiveScene().path
(which is allways unique) since LoadScene
takes 另外,您也可以尝试使用
GetActiveScene().path
(始终是唯一的),因为LoadScene
需要
Name or path of the Scene to load.
要加载的场景的名称或路径 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.