Search Unity

[Mobile] Instanciate vs enable vs engine

Discussion in 'Scripting' started by yls, Dec 6, 2012.

  1. yls

    yls

    Joined:
    Apr 13, 2012
    Posts:
    197
    Hi everyone,

    I'm developping a 2d plateformer (Yet an other one, yep ...). It's working fine with an infinite level (the plateforme are instanciated on the fly and destroyed shortly after).

    Now i want to make custom levels. I have a nice script reader able to instanciate everthings i need. I'm aiming for mobile so i don't have much memory and as i have unity basic i don't have the profiler.


    What is my best option between :

    1) Don't mind :
    Instanciate everythings in the beginning and never destroy them.

    2) Instanciate on the fly and destroy : when the player approachs an obstacle, i instantiate it on the fly. Sometimes i check for every object, test the distance from the player and destroy it if they are too far.

    3) Instanciate on the fly and disable: when the player approachs an obstacle, i instantiate it on the fly. Sometimes i check for every object, test the distance from the player and disable them it if they are too far.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    1, kind of

    here's the thing, creating a GameObject is rather expensive. You'll notice tiny slow down or frame skipping when you instantiate on the fly.

    What I do is I create a 'pool' of gameobjects that could be used in the level. The pool is a certain size (each type of gameobject gets its own cache size), but if I reach the cap of it, I add a few new ones (each type of gameobject gets is own buffer size to grow by). Then if the pool shrinks back down I can clean out the buffer size if I feel it should be.