- Home /
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.
I have found this link
http://answers.unity3d.com/questions/56102/methods-to-avoid-game-freezes-when-working-with-fo.html
But don't know how much this become useful to me.
Would you happen to be created a lot of things in the Start of your scripts?
If it is the case, you may want to consider using a loading screen (GUI or GUITexture) to hide the loading, and you keep it on for like 3s with all control disabled and then you remove the GUI and allow the game to run normally.
@siddharth3322 I don't think that the method described under the link is useful - in theory, it can help, yeah, but I would avoid this solutions, because:
It becomes much harder to follow this rule when project becomes bigger.
It moves some of the freeze reasons from first frame to the second, but some freezes will be still visible. It's just distributed between two frames.
I believe the biggest freezes are caused by physics which simulates all the objects before the physics calms down.
I don't believe you can avoid the freezes completely. But the solution of @fafse is one I recommend - display static color or image for first few frames before everything calms down.
On top of that, the loading screen makes your game look professional :)
Thanks friends all these suggestions. I will analyse all these things and reply you shortly.
Answer by Aithoneku · May 30, 2014 at 08:00 AM
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:
When a scene is loaded, display black screen (or any other color or static image). Game is paused.
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.
Fade into the scene.
Unpause the game. Now player can play.
Now scene is played. When you want to load other scene:
Pause the game.
Fade out (to black screen or anything else).
When the screen is completely faded out, load new scene.
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.
Thanks for all these master suggestions. Now I will check for this and reply you shortly.
At present, I am using Application.LoadLevel() method. I have to try for other one or not?
In order to create a loading screen, you need simply to start a coroutine that will display the texture over the whole screen for a deter$$anonymous$$ed amount of time.
void Start()
{
guiTexture.enabled = true;
StartCoroutine(LoadingScreen());
// Plenty of code to create and place object
}
IEnumerator LoadingScreen()
{
$$anonymous$$anager.SetState(State.Loading);
float t = 0;
while( t < 3){
t+= Time.deltaTime;
yield return null;
}
Destroy(guiTexture);
$$anonymous$$anager.SetState(State.Running);
}
This is a simplified version, the state is used in all scripts to check when you should be able to start playing:
void Update(){
if($$anonymous$$anager.GetState() == State.Loading)return;
// Plenty of code with Input and movements.
}
Thanks @fafase, I will try this thing and reply you. I think you have to put it as answer to help other members also.
Your answer
Follow this Question
Related Questions
Move Object Using iTween 2 Answers
Main character always attached with ground 1 Answer
Waves of birds 0 Answers
Drag and throw ball 1 Answer
Black Trail Rendering in 2D game 1 Answer