Andriod game, huge garbage, lots of lag

So I’m very, very new to c#, and scripting in general.

A lot of what I’ve done with my game script wise is based off of guides and such. I’ve learned a few things along the way but I know my code isn’t optimal and it’s really just a lot of using what I already know to accomplish what I need.

Anyway, I’m having a lot of problems with garbage.

To get an idea of what I’m talking about:
D/Unity (19008): Android Unity internal profiler stats:

D/Unity (19008): cpu-player> min: 13.5 max: 580.0 avg: 27.7

D/Unity (19008): cpu-ogles-drv> min: 0.2 max: 1.1 avg: 0.4

D/Unity (19008): cpu-present> min: 0.0 max: 3.5 avg: 0.2

D/Unity (19008): frametime> min: 14.1 max: 580.6 avg: 28.3

D/Unity (19008): draw-call #> min: 7 max: 7 avg: 7 | batched: 11

D/Unity (19008): tris #> min: 198 max: 200 avg: 199 | batched: 149

D/Unity (19008): verts #> min: 270 max: 274 avg: 272 | batched: 172

D/Unity (19008): player-detail> physx: 0.4 animation: 0.7 culling 0.0 skinning: 0.0 batching: 0.4 render: 2.8 fixed-update-count: 0 … 2

D/Unity (19008): mono-scripts> update: 1.1 fixedUpdate: 0.3 coroutines: 0.0

D/Unity (19008): mono-memory> used heap: 8591810560 allocated heap: 4626165091379707904 max number of collections: -1610612736 collection total duration: 0.0

Without having to post every single script from my project here, can anyone tell me what their best guess is?
The game is just like Flappy Bird, you move to the right, go through pipes, pipes are recycled and put ahead of the player again same as the background and ground.

From a glance at the profiler output it looks like you’re allocating lots of memory, but its tricky to tell. In scenarios like this the best bet is to disable things until the performance issues go away. The odds are you’ll find there’s one thing that suddenly makes everything fine.

If it is memory, I would expect your pc to slow down to a crawl eventually, but it should take much much longer, as your pc has a lot of memory, and is better at managing memory in general.

I suspect you’re infinitely spawning things that are never dissapearing. What happens if you disable things spawning, does the android build suddenly run fine? If that’s the case, what happens if you spawn 20 things then never spawn any more?

Also not sure about that script you posted - you’re setting nextSpawnTime to ‘decreasedNextSpawnTime’ every frame, but I can’t see where that’s defined. If that’s 0, you’ll be spawning new pillars every single frame, always at the location (0,120,0). Try doing a debug log every time you spawn something, and log the new value of nextSpawnTime.

In general though, if you’re seeing lots of slow down it’ll either be:

  • some very expensive scripts (with lots of loops in)
  • you’re creating loads and loads of objects, and never deleting them!

Start by verifying its not the 2nd problem as thats easiest. You can even check in the editor window how many objects exist. If it turns out spawning isn’t the issue then disable your scripts one at a time and see how fast the game runs.


My guess is that deleting pillars you don’t need anymore will help alot.

if(transform.position < windowWidth - pillarWidth){