Home>
What's happening

IEnumeratorAt the innerInstantiateIf i do a lot, the Profiler Scripts value will be very large.
Destroy ()OrInstantiate ()Is famous for heavy processing, but even if the frame is delayed, this value will gradually increase.

code Process without delaying the frame
void Start ()
{
    Start Coroutine (name of (Generate));
}
IEnumerator Generate ()
{
    for (int i = 0;i<1000;i ++)
    {
         Instantiate (new GameObject, transform.position, Quaternion.identity);
    }
}


Process with delayed frame

In the code aboveyield return null;I just added.

void Start ()
{
    Start Coroutine (name of (Generate));
}
IEnumerator Generate ()
{
    for (int i = 0;i<1000;i ++)
    {
         Instantiate (new GameObject, transform.position, Quaternion.identity);
         yield return null;
    }
}


A few seconds after the start ↓

Dozens of seconds after the start ↓


Process without delaying the frameThe code in will freeze the moment the scene starts, and after a dozen seconds it will thaw and you can play the game comfortably (at least the Profiler Scripts won't grow abnormally).

Process with delayed frameIn the code of, the scene starts immediately after loading the scene, and at first it was at the limit of 30 FPS, but it does not freeze and the player does not have to wait.
However, after a few tens of seconds, the FPS will start to fall below 10.

I want to ask

InstantiateIt is understandable that the value of Scripts will temporarily increase as a result.
And it's understandable without even thinking about freezing when it's required to generate 1000 GameObjects at a time.

However, while giving some room to the CPU (yield return nullI don't understand that the value of Scripts is gradually increasing even though I am creating a game object (by adding).

Not surprisingly, as the number of game objects increases, the rendering process takes longer.
As far as I can see from the profiler above, the cause of the low FPS is Scripts.

Does this happen because the object isn't created in time?

Why is this value increasing little by little without moving in parallel (keeping the same value)?

Please answer somebody.

  • Answer # 1

    The components contained in the created object were rampaging.
    I optimized it and solved it.