Search Unity

Instantiated prefabs causing slow down

Discussion in 'General Discussion' started by Vanz, Nov 2, 2022.

  1. Vanz

    Vanz

    Joined:
    Nov 19, 2013
    Posts:
    374
    I have these blood platters that on occasion I instantiate the preb, they animate then destroy themselves. In VR they show up as these small FPS spikes, that I think is due to them being loaded. I randomly cycle through 15 of these animations, how would you handle this to get rid of the lag spike? If I preload them, they will animate and be destroyed. I was thinking of going into their scripts and turning off the animation until I need it and preloading them but was wondering if there is a better way?

    Thanks,

    V
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,627
    Are you using the profiler? It sounds like you haven't identified what aspect of system is causing the slowdown. For anything running on the CPU you should be able to see a break-down of every bit of code and how long it's taking, so you can find the precise action that is taking the most time. I think you need to dig deeper.

    If it turns out that instantiating these objects causes the spike, you could use pooling I.e. Instantiate a pool of 20 splatters at the beginning of the scene and deactivate them. Instead of Instantiating and destroying new objects every time, keep activating and deactivating the same objects instead.

    Definitely use the profiler, though. If you try to guess what is causing the slow-down then
     
    Last edited: Nov 2, 2022
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,826
    Sounds like you should use object pooling. Check out Master Object Pooler in the store (free).
     
  4. Vanz

    Vanz

    Joined:
    Nov 19, 2013
    Posts:
    374
    yeah, I get frustrated with the profiler, it takes so long to get it to build through my VR headset and then run through the profiler... it also does not work every time either, its flaky for me, some times it runs other times it looks like its running but doesn't update profiler (when trying to run through the headset), it's a big time sync... I run OVR metrics on my VR headset and see a visible small spike every time I see a blood splatter, so I could spend several hours double confirming it with profiler or act on it... thanks for the pooling idea kdgalla...
     
  5. Vanz

    Vanz

    Joined:
    Nov 19, 2013
    Posts:
    374
    thank you
     
  6. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,697