We have a small menu scene that runs when the game starts. This then calls the main Play Game scene. The Play Game scene is where we have a script which defines most of the assets and the corresponding variable. Most of our objects are instantiated dynamically via scripts.
The problem we’re running into appears to be memory related. On the devices older than a 3GS, the game crashes when loading the Play Game scene. It works for 3GS and up devices.
What’s confusing is that the Play Game scene doesn’t actually create many objects at first: You start inside a space station interior, so it’s just your ship and the interior model until you launch into space. So it would appear that the sheer number and size of all the music, sound effects, ship models, etc. are what’s causing the low memory.
Is there a way to improve the memory footprint using our big scene approach? Or do we need to rethink the architecture? Do we need to manage things using Resources.Load()? Or would that even help?
Any pointers would very much appreciated as it’s not clear to me via the documentation how Unity deals with resources that are defined in a script, but not instantiated or used.
All the assets you have referenced in the scene (via public vars etc) will be loaded into the memory when the scene loads, regardless of whether they’re actually instantiated or not. You’ll need to use Resources.Load() to load the resources if the memory footprint is too large. You’ll also need to unload unused resources with Resources.UnloadUnusedAssets() (just Destroying them doesn’t remove them from the memory) to free memory. Unloading and loading resources will cause some jittering so you’ll probably want to use some kind of loading screen for that.
Thanks trooper and arzi, we’ll definitely look into those things. In particular, it looks like we’ll need to re-architect and use Resources.Load() instead of stuffing variables. A shame that Unity couldn’t be smarter about things (or let us provide hints about which variables to load and when).