- Home /
My mobile game crashes once a certain number of textures have been loaded. What can I do about that?
I am developing a mobile app, and I have a single GameObject with over 440 different textures on it. The game runs fine in the editor, but when I export to a device and attempt to run the game, the game crashes once it reaches the scene with the 440 different textures.
I guessed that Unity didn't like one object having so many texture son it, so I attempted to solve the problem by dividing the GameObject into two objects which each have 220 textures. That didn't work; the game still crashed once both were loaded.
I figured that maybe if I had the two objects in two different scenes, it would be okay. I put one 220-texture GameObject in Scene 1, and another 220-texture GameObject in Scene 2, but the game crashed when going from Scene 1 to Scene 2.
So then I wondered if maybe the game would completely unload the object from Scene 1 if there was an intermediary scene. I transitioned from Scene 1 to Scene 1.5 (blank scene) and then to Scene 2, and still got a crash.
It looks like, once I've loaded a certain number of textures, the game will simply crash. I don't know if there's a specific number that is pissing Unity off, or if I'm simply running out of memory on my device.
Either way, what is the solution to this problem? If I want the user to be able to see 440 different textures, what should I do to prevent Unity from crashing?
Answer by flamy · May 20, 2012 at 03:46 PM
Basically when you create a Texture2d or Texture object and link a texture to it,that means that texture will stay in memmory until the object is released. so basically you have 440 textures in the ram at the same time. if a texture is 0.5f mb (for example) you are using up 220 mb of ram + unity run times, other assets + other application, so basically you app is forced to quit. If it is iphone there is a limit for ram ur app can use. whn this is exceeded you app will be shut down automatically.
I am pretty sure at a single frame you are not going to display all 440 textures. so what you have to do is keep the textures in resources folder and in script instead of having 440 texture or texture2d objects, have 440 strings, that stores path for each texture. and when you need it call the Resources.Load() function to get the texture. and also call this function whenever you can to keep memmory in check.
That solution worked perfectly! Thank you so much for your answer! I'm back in business!
@flamy : Hey flamy, I am facing the same problem if I load so many textures using Assetbundle. How do i solve this?
@flamy : Hey flamy, I am facing the same problem if I load so many textures using Assetbundle. How do i solve this?
@flamy : It didn't worked for me.
I am using below code to get textures in my scene-
BGForest.GetComponent ().sprite = (Sprite)Resources.Load("Texture/$$anonymous$$apScreen/$$anonymous$$apScreenBG/$$anonymous$$ap_image1_forest2",typeof(Sprite));
and using following code to unload unused assets when new scene loads - Resources.UnloadUnusedAssets();
$$anonymous$$y game crashes after running 5-6 $$anonymous$$utes. Scenes slowly become heavy and xcode starts giving memory warnings and at end game crashes.
Any way out?
Hey, i've got similar problem. Did you resolved it?