Search Unity

Instantiation lag & Memory

Discussion in 'Editor & General Support' started by jasonoda, Jun 25, 2020.

  1. jasonoda

    jasonoda

    Joined:
    Sep 7, 2012
    Posts:
    67
    We have a game in which periodically several objects need to be instantiated mid-game. On older versions of the Xbox. This creates a giant lag the first time these objects are instantiated.

    Before I go any further, I know the answer to this question is create an object pool, but for various reasons, this is not possible in this situation.

    When these objects are instantiated, the first time the giant lag occurs, but the second time, the lag does not happen. I'm guessing this is because the objects that have been instantiated have entered the memory or something.

    So to solve this, I pre-loaded all of these potentially problematic objects during the level load. Meaning I instantiated them once off to the side, let them sit for a couple frames, and then .setActive(false) on all of them. So they're there. Off screen just hanging out.

    Problem is: during play the problem still persists! First time still lags. Any idea why this fix doesn't help things?
     
  2. jamespaterson

    jamespaterson

    Joined:
    Jun 19, 2018
    Posts:
    401
    How complex are the objects? What are inside them? I have seen noticeable lag toggling setactive on complex objects with lots of (small) mesh colliders due to physics warm up.
     
  3. jasonoda

    jasonoda

    Joined:
    Sep 7, 2012
    Posts:
    67
    They're relatively simple. Just a couple simple shapes and 2-5 gameObjects. They all have a meshCollider, a rigidbody, and an audioSource.
     
  4. hjohnsen

    hjohnsen

    Joined:
    Feb 4, 2018
    Posts:
    67
    Perhaps sounds decompression, but It's perhaps texture loading when your objects are first visible, because I don't think textures are loaded at the same time of the prefabs.
     
    jamespaterson likes this.
  5. jasonoda

    jasonoda

    Joined:
    Sep 7, 2012
    Posts:
    67
    Hmm. Ok, perhaps what I need to do is instantiate the objects in front of the camera while the loading screen is up. I'll give it a try. Thanks for the advice.