Game runs fine first time but lags when stopped and started in editor.

I am having a problem when running my game in the Unity Editor. The first time I run my game it will perform as expected. When I stop and start the game using the ‘play’ button within the Unity Editor it will run extremely slowly. If I close the Unity application using the ‘X’ window button and restart the application the game will again run OK the first time and then lag if restarted using the ‘play’ button.

I am working on optimizing the performance of my enemy objects. To test their performance I spawn 50 enemies one at a time with 0.5 seconds in between each spawn.

The first time I run the game it will gradually slow down to 30-40 FPS* as the 50 enemies are added. Once all the enemies are added the frame rate will stay stable.

The second time I run the game (after stopping and starting with the ‘play’ button) the game will very quickly begin to lag as enemies are added. When it reaches 25 enemies the frame rate will be <1 FPS.

If I exit the Unity application in Windows and start it again then the game will run fine again the first time and lag when restarted.

There are no errors or warnings in the console.

My game makes use of the DontDestroyOnLoad() method in order to create a static enemy pool. Enemies are created in the first scene of the game and re-used rather than created and destroyed. I used the ‘Persistent Singleton’ pattern for this pool, which is described here: Loading...

I have posted this problem on Reddit and tried some troubleshooting already. You can read the thread here for more information: Reddit - Dive into anything

The problem has occurred in both Unity 4.5 and 4.6. I have tested the project on another computer and encountered the same problem. I don’t have any third party editor plug-ins or scripts.

I suspect some data is persisting between runs in the editor. Does anyone know of anything that could cause this behaviour?

  • I don’t know if it’s relevant but sometimes it will slow to 30 FPS and sometimes (on different days) it will only slow to 50 FPS with no known change in background applications running.

I’ve had similar problems with unity editor performance. The bottleneck was the running profiler. Or i should say the reason was poor development machine.