I have programmed a game for Android. The frame rate drops from 60 to 30 really quickly and back up again, resulting in a lagging game. I have set
Application.targetFrameRate = 60; but that still doesn’t work. Sometimes I have a constant 60 but only for a few seconds and then it starts dropping again. Any ideas?
Maybe any certain commands or stuff in my scripts which could cause it?
It sounds like you have some script/behaviour that is taking a little bit more processing power to calculate so the game is getting held up slightly before processing the next frame. Make sure you check and loops (for or foreach, etc) that is happening in any Update() or repeating functions. Loops greater than say about 100 with lots of actions can slow things down. If it’s an essential process doing it, maybe look at options to do it in chunks. If you have a for loop that goes thru 1000 times, do it in lots of 100s - just an example. Would need to see the code to help out more. Hope this helps mate.
For loops dont necessarily slow the game down, its what happening inside them that does. Try commenting out all the code in the for loop and see the performance difference.
Dont take stabs in the dark regarding performance issues.
Use the Profiler and see what is happening on the CPU.
Look at what the scripts are doing and what the Garbage Collector is doing. Im not an expert on Garbage Collection but what i do know is periodically (possibly when a threshold is passed) the GC will free up memory that your game is using but no longer has access to it, e. g local variables in a method. The GC will block the main thread so it can do its thing, which is what causes most lagg issues.