Strange lag when instantiating new objects

Hey guys.
I’m facing a strange issue in my game.
I’m working on a 3D runner game when new obstacles are being created every few seconds. When an obstacle is created, I put it somewhere out of player’s sight and after that obstacle moves toward the player and comes into sight.
Every thing works fine except that when the obstacle begins to pass the far clipping plane and begins to come into player’s sight, there is a strange lag and after the object is completely in sight, the lag goes away completely.
Can someone tell me what the problem is?
I even put an instance of each obstacle in the scene (above the player, somewhere that player can never see) in order to reserve the memory for obstacles too. But the problem still remains.
Thank you

Do you have unity pro? Than you can use the Profiler to figure out what is eating you frame time.
If not , do you see anything “abnormal” in the Stats widow if the stutter appears?

Yes I have unity pro.
Alright I will test unity pro to check what is the problem.
But I didn’t see anything abnormal in the Stats window.

It seems that the most intensive computation is regarded to:
Camera.Render->Drawing->Render.TransparentGeometry
Environment in our game is some kind of a forest. So we have lots of leaves hanging from trees and mountains. Leaves are all planes with some transparent texture on them. Could it be the reason? I don’t think rendering transparent planes would is a difficult thing to do!
Any ideas?

PS: Here’s the profiler result shot.

If the trees and leafs would cause this, it would not be a spike but the whole time they are visabel.
Are you blending in some object?
What do you find if you dig deeper into the profiler?

If by blending you mean merging objects, no I’m not blending any objects.
I didn’t find anything more informative from the profiler. Here is another shot of profiler:

I ran a deep profiler and I found out that this lag is because of my coding.
I’m coding a 3d runner game where all obstacles are created every 10-20 seconds. It seems that instantiating objects is causing this lag. Does anyone know how to solve that?
I even put an instance of each one of my game objects in the scene so that they are already in ram. But the problem still remains.

The magic word is pooling.
Just instantiate all objects you may need at the start into the scene and have them deactivated.
Then if you need them you can move them to the correct position and activate them.
I did it for a iOS game for the iPhone 3GS and it worked fine.

1 Like

Yeah I thought of that too. But in runner games you can’t say how many of each objects are needed.
Idea?
Although I can for example instantiate 20 instances of each object and use those anytime I need. Does that seem right?

The question is how many of what type are possible viable on the screen at once?
If the used parts getting off screen , due the runnier has run past them, you deactivate them and add them back to the pool.
You can recycle them as often as you like.

Yes right now I destroy objects when they go off the screen.
Your solution seems to be reasonable.
Thank you :slight_smile: