- Home /
Not clear how Application.LoadLevel works
In my game I have several scenes. When I load scene 2 from scene 1. I can see some of the textures used by objects in scene 1 is still in memory in scene 2 (using profiler).
I am trying to understand why this is happening.
If I have list of objects in scene 1 and I load scene 2 without clearing the list or there is an object reference I used and I didn’t set it to null can scene 1 object comes to scene 2?
Is there a way to figure out this using the profiler? I can see the texture is in memory but I don’t know who is casing this.
By defaults every GameObject you loaded in a scene will be destroyed once you move to a new scene unless you use an additive scene loading or if you mark any gameObject using DontDestroyOnLoad().
Well, textures are actually cashed in the memory. When you switch scenes they may still be in memory, they are not bound to game objects, game objects just use them. If the engine needs to allocate some memory for textures it might overwrite this cached memory if the memory is not used by any game object in the scene.
Also I wanna note that static variables maintain their values across scenes, if these scenes use the same script in which the static variables are. Just be careful with static variables, especially with events. For any subscriber in the Start() method or anywhere else, you need to unsubscribe the event in the OnDestroy() method.
When I exa$$anonymous$$ed it in profiler I saw Assets/GameOBject section and some of the GameObjects used in previous level appear there. None of thedr gameobjects are in new level.
At the end of the “Referenced By” panel of those gameObjects I saw a line $$anonymous$$anagedStaticReference()
Does this mean a static variable in the game not get released properly?
The way I load levels are; Load empty level with Application.LoadLevel then load the actual level with asynchronous loading. Will that cause a problem like this?