Took so long time for entering play mode

Suddenly it started to take so long time for entering play mode yesterday.

Like it take over 5 mins for both entering and exiting play mode.

I don’t know why and nothing special changes were made from yesterday.

Just like normal works, script revisions, etc assets added, so on.

What is problem then?

now at exit the play mode, it take forever too. (5 mins)

Funnily enough someone posted this same issue on the Editor & General Support subforum (where this should’ve been posted as well). I wonder what’s in common between your set ups that’s causing this.

On our project, we’re on 2 minutes to enter and 2 minutes to exit play mode. It seems directly proportional to the number of objects in the scene. Unity reloads the scene when entering play mode, and again reloads the scene when exiting play mode, but it still seems crazy long. I can reduce it to 30 seconds by simply removing objects from the scene (not disabling, but deleting completely).

For exiting play mode, I have no work-around - but, for entering play mode, you can set the “Enter Play Mode” options (Edit → Project Settings → Editor → Enter Play Mode Settings). Here, you can uncheck the Reload Scene option. However, this has major side effects, as your Awake (and Start?) callbacks won’t run when you begin play mode. And as stated, it will still take a long time to exit play mode.

I’m working on this now, and will report any further findings/fixes.

1 Like

That’s just wrong! Those methods for sure still get called.

You should turn off both Reload Scene and Reload Domain if you can. Turning off Reload domain requires you to write code differently, as static fields will not be cleared on entering play mode anymore. So either avoid them, or when you need them, use a [RuntimeInitializeOnLoad] method to clear them.

Turning off Reload Scene is less of a big deal - it means that values the objects in the scene currently has will still be there, but that’s seldom a problem.

It is, by the way, very possible to diagnose what’s happening when you enter/exit play mode that’s taking long. Turn on the profiler, turn on deep profiling, turn on editor profiling, and press play. Sort by cpu time, expand until you find something that’s taking long that seems familiar.

3 Likes

Correction… if you have ExecuteInEditMode attached to a behaviour (which we have a lot of), Awake() will not be called again when entering play mode without reloading the scene. This makes sense, but also makes the setting unusable.

On top of that - it does not solve the 2 minute delay when exiting play mode (which again reloads the scene).

FIX (work-around):

Regardless - I have found a work-around and I have also identified the condition causing the extreme delay. However, I don’t have a good explanation for why this behaviour is exhibited in the first place:

We have a complex scene with 10K+ prefab instances placed into the scene - rocks, trees, pebbles, flowers, etc. So I found that entering/exiting play mode time was directly proportional to object count. What I did to fix it, was to simply place all prefab instances in a prefab parent. For example, all the tree prefab instances are now under a TreeParent, which itself is a prefab.

Enter/exit play mode is now no longer linked to object count, and takes a constant 15 seconds, regardless of how many trees and rocks are within the parent prefab.

So, there is clearly something messed up in the way the Unity deserializes and/or instantiates prefab instances that are placed directly into the scene hierarchy. Like I said, I have no explanation for why this should be, but the issue is resolved for now.

7 Likes

I know its a bit late on this discussion, but I wanted to leave a response in case any one else was having this issue. For my project particularly, it was a real simple problem. I placed down 2K trees, which are parented to the terrain in Unity. I tested it, and the play mode load time was pretty quick as usual (just a few seconds). Then I created an object pool for NPC’s that I am using, and this pool is only 1K. I ran play mode and it took about 1 minute to load (A big difference). I was trying to figure out why because it wasn’t making sense. So I tried parenting the NPC object pool to the terrain to see if there was some sort of difference in the hierarchy loading approach within Unity. This did not affect the loading time and it remained at 1 minute.

I personally feel object pools are much more efficient. Using Instantiate, TryGetComponent, and so forth can be very expensive and cause noticeable performance hits at run time.

Findings: It has to do with the components. In my case the trees and the NPC’s utilize a Settings class(script). Both have the script so it was not the case after confirming. The main difference was the NPC’s are utilizing AI Nav Mesh components, i.e., nav mesh agents. The trees are using nav mesh obstacle components, but the culprit seems to be the nav mesh agents loading at run time. For the terrain at this time the NavMeshSurface component is still experimental on the version I am using 2022…3.7f1.

Solution: Instead of using a pre-allocated object pool, I decided to allow for a bit of time during run time to do this. This can be done at the very beginning (not recommended without notifying the player as it could lead to long load times), but I suggest having a main menu, and then once the player is choosing to enter the game, you can create a simple loading screen to notify the player that their “world” is generating. This should be a really quick process when done this way, of course, it depends on the players machine setup, but I think you get my point. :wink:

I hope this helps someone, and prevents you from searching the entire web for a solution. Your specific case may differ, and this approach may not work, but it might! Much Love!

3 Likes

for my problem, it is because i forgot to add increment in forloop

i used this Probuilder Function on the screenshot on some objects
the only solution was deleting them or creating a new pro builder mesh and remodel the object ;D
it solved the long loading times when stating or stopping the game.
Screenshot_1