Game becomes choppy after gameobjects deactivated then activated

Hi,

Not sure if this is the best place to post this question, but hopefully someone has an idea of what is happening.

I have a number of gameobjects in a 2D game which have various child gameobjects. Each gameobject, together with the child gameobjects form part of a scene (a frame).

The ‘frames’ activate and deactivate as the player moves across each of them. However, I am finding that as the player moves back and forth between the ‘frames’ the rendering becomes choppy, notwithstanding that it is ok the first the player moves across each ‘frame’.

I cannot figure out what is causing this, any ideas? Are the deactivated objects affecting performance?

Thanks

Hello @kdrago ,
My suggestion would be to check the profiler to get a better understanding of what is eating up your frame time.
You can find the profiler at Window > Analysis > Profiler.

Here you can read a bit more about what the different charts mean: Unity - Manual: CPU Usage Profiler module

Hi, thanks for the reply.

I have gone through the profiler at length, and the only spike I can find comes form Render Camera, but I am not sure what the connection is given the way that the drop in FPS occurs according to the stats in the editor.

It seems that every time the player activates the objects in a frame the fps drops when the player enters the other frame. Meaning that if the fps was 120 in frame 2 when the player goes to frame 1 and returns to frame 2 the framerate drops to 90, if it is done again it drops to 70 and so forth.

I have not been able see anything in the profiler that explains this, but it suggests that despite the objects in the other frame being deactivated, they are having an impact on the game, which increases every time they are deactivated and then activated.

BTW I cannot use development build. I get the following error:

“Dimensions of color surface does not march dimensions of depth surface”

I should add that I am using the URP, with 2d Lights

When you say frame, is that rendering frame or scene?
If you are seeing the frame rate decreasing at a steady rate then this should also be visible in the profiler, where some method(s) increasingly take up more frame time.

Any recording or screenshot of the profiler with this issue would be helpful to understand what is going on.

Hi, apologies the wording used in my post is unclear.

Each level of the game is a Scene. The level is divided into separate segments to preserve performance. Each segment is comprised of a gameobject with a number of child gameobjects which make up that part of the level, e.g. the camera, platforms, 2d lights and other objects. The setup is similar to that shown in this video (see minute 27 of the video)

.

The video describes each part of the level as a screen box. Note, I am using a new camera for each ‘screen box’ (the previous camera is deactivated together with the other gameobjects every time the player moves to a new ‘screen box’). I tried using one camera with multiple VCams (cinemachine), but this didn’t resolve the issue.

When the player moves from one part of the level (screen box) to the other, which deactivates the previous part of the level and activates the current part i.e. the part in which the player is in, the framerate is steady. However, if the player goes back and forth a number of times activating and deactivating the various parts of the level the framerate starts dropping significantly.

Below is the profile when the player changes from the first part of the level to the second part. The frame rate is between 65 and 70 fps (Profiler 1) . When the player goes back and forth deactivating and reactivating the first and second part of the level a number of times the frame rate drops to 35 fps (profiler 2).

This happens even if a gameobject with the child gameobjects forming part of the level (a screen box) is deactivated and activated without the player moving into any other part of the level. Moreover, it only appears to happen if the 2d lights are being used, which have become essential. However,


Thank you for both the in-depth explanation and the profiler screenshots.
Looking at the screenshots, the GPU goes from around 10ms in the first screenshot to around 20ms in the second screenshot. If we also compare the batches, we can see that the batch count more than triple. This seems to be the issue for your lowered frame rate.

Have a look if you are disabling all objects that you think you are, and disabling the cameras that you believe you disable. The frame debugger is a great tool to give you an overview what you are rendering each frame. Have a look to see if there are objects being rendered that are supposed to be disabled.

Many thanks, the frame debugger was very useful in identifying the issue.

I have uploaded the information displayed by the frame debgger, and I have set out an explanation of the steps I have taken and what appears to be the position.

If the lights and the sprites are deactivated and reactivated Unity appears to render the lights again on top of the original render (this may not be technically what is happening). This can be seen from the information provided by the frame Debugger.

For example, if I expand any of the Render 2D Lighting and click through the various components, there comes a point when they keep repeating. There is also a clear increase in numbers, e.g., the first Render 2D lighting is 82 and after a few times deactivating and reactivating the ‘screen box’ that contains the lights and the sprites, the figure increases to 370. The same is true for all the other Render 2D Lighting. This occurs when:

  1. the gameobject with the Light2D component is deactivated and then activated as well as the Sprites; or

  2. both the gameobject with the Light2D component and the Light2D component itself are disabled and enabled as well as the sprites.

However, if only the gameobject with the Light2D component is deactivated and reactivated i.e., the sprites are not also deactivated, this does not happen. Unfortunately, this is not an option since not deactivating the sprites impact on performance.

On other hand, if the gameobjects containing the sprite renderer are disabled but the Light2D components are not disabled, the same issue arises.

It appeared that if the Sprite Renderer component only of all the gameobjects was disabled, but not the gameobject itself, and the Lights were all disabled, the issue did not seem to arise when they were then enabled; however, it turned out that this did not make a difference and the issue still occurs.

Therefore, whilst the frame debugger shows that the gameobjects are all being deactivated and activated, the lights are being rendered again each time that the gameobjects that contain the sprite renderer component as well as the 2D lights are deactivated and activated. This is the cause of the fps drop, but I do not know how to resolve this issue since it would appear to me that it may be caused by the way the light2D component and/or the 2d renderer render the lights. Using version 2019.4.17f1, and I have tried URP 7.5.2 and 7.3.1.

Great that you tested this out. Seems like there is an issue on our end. Could you file a bug report and post the bug ID in this thread? Then we can take a look at this issue.

Thanks!

Hi, will do. However, it seems that the issue is caused by the shadow caster component. If the shadow caster component is disabled the issue does not appear to occur.

The report number is 1305092. I created a simple scene to show the ‘screen box’ system and the how the issue occurs. There is also an explanation in the bug report.

Awesome, thanks a lot for the report and the repo project. We’ll have a look and let you know how our investigation goes.