简体   繁体   English

Unity OnLevelWasLoaded没有被调用

[英]Unity OnLevelWasLoaded not being called

I have a script called StartGame that is suposed to do different things when different game modes are selected. 我有一个名为StartGame的脚本,当选择了不同的游戏模式时,该脚本可以执行不同的操作。 Unity's API and forums suggests using OnLevelWasLoaded, but it isnt being called. Unity的API和论坛建议使用OnLevelWasLoaded,但不会被调用。 In fact when I hover my mouse over it in Visual Studio it tells me the method is"StartGame.OnLevelWasLoaded". 实际上,当我将鼠标悬停在Visual Studio中时,它告诉我方法是“ StartGame.OnLevelWasLoaded”。 I'm pretty sure that means that It isn't overriding the method. 我很确定这意味着它没有覆盖该方法。 Has anyone else had this problem? 有没有其他人有这个问题? Also I'm using Unity 5.3. 我也在使用Unity 5.3。

Are you sure your GameObject is still existing after loading the new scene? 确定加载新场景后,您的GameObject是否仍然存在? Usually, all GameObjects are deleted from the scene when loading another scene. 通常,在加载另一个场景时,将从场景中删除所有GameObject。

You can prevent this by loading the scene additive (adding the contents of the new scene to the contents of the current one) 您可以通过加载场景添加剂(将新场景的内容添加到当前场景的内容中)来防止这种情况。

SceneManager.LoadScene("your scene name", LoadSceneMode.Additive);

or by preventing deletion for your specific GameObject 或通过防止删除您特定的GameObject

GameObject.DontDestroyOnLoad(yourStartGameScriptHolder); // where yourStartGameScriptHolder is the GameObject(!), not the script reference

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM