[SOLVED] OnTriggerEnter2D first collision causes lag/freeze

Hey all, I’m currently developing a mobile game and I ran into a small problem. When my player first collides with an enemy, the game freezes for a bit.

The weird thing is that this happens only when first time OnTriggerenter2D is called. Enemies that collide with my player afterwards do not cause any lag. I profiled the game and it shows that three things cause this issue, specifically: Physics2D.CompileContactCallbacks, Physics2D.SendContactcallbacks and Physics2D.SendTriggerContactCallbacks.

My player has a dynamic rigidbody2D attached as well as BoxCollider2D that has set OnTrigger to true. The same applies to the enemies, except the rigidbody2D is set to kinematic.

Here’s a screenshot from the profiler: Imgur: The magic of the Internet

I’m testing it on Samsung S4 mini.

Any ideas how to fix this?

Thanks!

The profiler seems to be indicating that it’s something that is happening in your Enemy.OnTriggerEnter2D script and not the internal methods you mention above. Something is taking half a second! I presume you’ve tried taking out all the code there to check this is the case?

1 Like

You’re definitely right! I was so tired yesterday that I didn’t even notice or realize that, I must be blind. After getting some sleep I found out that in the Enemy.OnTriggerEnter2D I’m using a method that saves picked up coins using (System.IO) File.Open that caused the freeze.

Thanks a lot for your time to answer my question I really appreciate that!

1 Like