Game Freeze on Start Up

Basically I am working on 2D game development.
When I start game, it will freeze two or three frames on start up and then work normally.

At present my game is in starting phase and I don’t know why this is happening?

So I provide details about what I have done in my project.
This game is vertical scrolling game. So I move my backgrounds in vertical manner one by one. My main character moves horizontally based on player touch.

On game play start, I provide constant velocity to my background and adding force to my main character for movement.

As per my consideration I have to put loading screen so that everything loaded for game and then resume game for actual playing.

On three devices I checked my game but no change in output. Per device I found different freezing that was strange for me. For poor configuration device, I found more poor behaviour.

If you have any idea about problem then please guide me in this.

This is just a guess, because I would need much more info (including the scene) to be sure, but I think this is highly probable;

when scene, sub-scene is loaded or prefab is instantiated, Unity needs to start these objects (initialize components), which includes:

  • Call Start() on the most of the components.
  • Call Awake() on the most of the components.
  • Register collides in collision system. (This might be done during loading - I don’t know.)
  • Load static meshes, textures, shaders, etc. to graphic memory. (This might be done during loading - I don’t know.)
  • More things depending on used components

Then, first few frames are really heavy:

  • Physics simulates all the object before they “calm down”. (I think this is the main reason of the freezes.)
  • Maybe your objects (depending on your implementation) registers their existence somewhere and look around themselves.

The freeze will be much longer with bigger scenes (more game objects and components).

I believe it’s unavoidable in the most of the cases, so IMHO it’s not possible to solve, but it’s possible to hide this problem and make the user experience fluent.

My solution, in last project I was working on, is through fading screen - it’s a game object + component which fades in and out when switching between scenes. When scene is loaded, I wait a few frames before fading in. Example workflow:

  1. When a scene is loaded, display black screen (or any other color or static image). Game is paused.
  2. Wait a few frames (I used 10). Now the FPS is really bad, but because you display static color or image, user doesn’t see this.
  3. Fade into the scene.
  4. Unpause the game. Now player can play.

Now scene is played. When you want to load other scene:

  1. Pause the game.
  2. Fade out (to black screen or anything else).
  3. When the screen is completely faded out, load new scene.
  4. When the new scene is loaded, black screen is displayed so user doesn’t see any problems. Again, wait few frames and fluently fade into the scene.

With this I was able to achieve 100% fluent transitions between scenes even when the scene was really big one with lot’s of physically simulated objects.