Scene sometimes loading twice

I’m having a problem where sometimes it appears that when loading a scene, it ends up actually loading the scene twice. My project uses a few separate scenes that stores entities with children and components attached to them (essentially recreating the functionality of Unity prefabs but instead of these existing as assets, they are entities in these “prefab scenes”)

When the project is run, I have a script which loads each of these scenes, adds each entity to a container object, disables the “master copy” entity, and then has some functions for creating new instances of that object to allow me to dynamically create entities from those master copies.

This works most of the time. However, sometimes (maybe 25-50% of the time) one or more of the scenes gets loaded a second time and the indexing / disabling of entities does not run on it causing all of the master copies to exist in the game scene and the lights from the prefab scene stack on top of the lights in the game and everything gets blown out. It’s not ideal.

I’ve confirmed that the scene is in fact loading a second time and have some ideas for workarounds but one of the big focuses on this project is minimizing load times so preventing the scene(s) being loaded twice in the first place would be much better than dealing with it when it happens.

I’m stumped on what exactly is happening. My best guess is that if the response for loading a scene takes too long it times out and sends a second load scene request and they both eventually resolve? maybe if that’s what’s happening the callback from the first request is getting lost along the way (this is where the indexing and disabling happens)?

Any insight would be greatly appreciated!

UPDATE : This appears to be some kind of an issue related to the linkage between the editor and the game being connected. To avoid having to refresh every time it happens, I added in an entity to each of the “prefab scenes” which has a script that stores the scene ID as an attribute. If it detects that the scene is loaded beyond the first time it just disables all the entities that are children of that scene’s root.

Hi @adam5000, I’ve been using for a while the loadSceneHierarchy method and didn’t have any issue with it.

Is it possible to post a link to a sample project that reproduces your issue to take a look?

Hey @Leonidas! thanks for the reply! I am using loadSceneHierarchy. I’m setting up a separate project that isolates the issue. It happens so inconsistently that it might take a few minutes to confirm that it is actually reproducing the issue. I will post a link to it as soon as I know it’s behaving the same!

1 Like

Follow up, It seems like in the isolated project I’m not able to reproduce this. I can’t link to the full project where I’m experiencing this due to it being developed for an actual release. I’ve only seen this occur when I launch the game from the editor though and thought that it might be an issue of multiple people having editors open in the same branch when the game was running. so I downloaded the project and hosted it on a server so it would not be linked to the editor and have not been able to reproduce it there. I do however experience it even if I create a new branch and am sure I’m the only one in it. I’m guessing it’s an issue of the editor being linked and something about how it communicates with the game when launched from it?

If it happens only when launched from editor and multiple people are using the same project on the same branch, any changes they make are reflected in the launched runtime.

I’m not 100% sure that messaging can be disabled :thinking: Can you try disabling ‘Debug’ from when you press the play button?

1 Like

Thanks for the reply @yaustar! I’ve experienced it both with ‘Debug’ on and off. The test with a separate branch was to see if it was the result of someone making a change in that scene while it was being loaded causing it to reload itself maybe but it still was happening there too.

I think I know what’s happening here @adam5000 . Is the scene loading for the second time after the first scene loads? In that case, can you try calling the this.changeScene function inside a check for a boolean variable like so👇?

var check = false;

if (check === false) {
    check = true;

@DevilZ I’m loading a list of scene ID’s into my start scene to have those entities available to me to duplicate from code so I just output a message to the console to see if it was calling the load function with the same scene ID twice. It’s calling it only once for each scene ID but I did notice this error in the console when it loads a scene twice. No error when it all goes smoothly and each scene is loaded once though.

Could you share a editor link?

Unfortunately no, I’m not able to share any of the project’s resources.

That does look more like a an editor only issue with the link / messaging system between editor and the launcher :confused:

@yaustar I can live with that! It’s a bit of a pain that it happens at all but it’s not the end of the world as it’s pretty easy to spot when starting the project and we can just refresh once or twice to get a load without it happening. It would be nice to have an option in launch to decouple the editor from the game but it sounds like I’m doing something pretty specific that the editor just doesn’t always handle well and other folks don’t seem to be running into this issue. The big worry was that it would potentially show up in the release version of the game but it sounds like that’s not a risk since it would not have an editor communication! I appreciate the help guys!