Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Problem with SetActive and Object Pooling

Discussion in 'Scripting' started by Narkata55, Jul 19, 2017.

  1. Narkata55

    Narkata55

    Joined:
    Feb 25, 2017
    Posts:
    63
    Hey guys, I'm hoping someone has encountered this problem and could offer some insight. Im creating a tile based game and recently put in object pooling for the move and attack tile gameobjects. Basically i put them out of the bounds of the map at the beginning and disable them. Then i set them active and move them as necessary, then put them back and deactivate when im done. This got rid of the lag from instantiating a medium amount of tiles, but even now when i do this process for a large amount of tiles, there is a noticeable lag spike. I think it has to do with setting them active because deactivating and moving is instantaneous (which makes it seem like moving transforms isnt the problem).

    Basically, how would you go about fixing this? I was thinking about just putting both an attack and move tile over every tile on the map and disabling and enabling the sprite renderer when necessary, but that seems like it would lead to tons of unnecessary game objects in memory (and if setActive is the problem, and not moving them, this would do no good). Thanks in advance, and let me know if posting any of my code would help in figuring this out!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Have you tried just moving the objects "far away" (maybe to position Vector3.up * 1000?) and not bothering to .SetActive(false) them?
     
  3. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    Do you have a lot going on in your OnEnable function(s), if you have one, for these objects?

    Have you tried running the profiler to confirm where the lag is? I would think that activating should be fine unless you have thousands of things activating at once. Maybe you've got something complicated happening that you haven't noticed when your object's scripts enable.
     
  4. guillermoi

    guillermoi

    Joined:
    Sep 20, 2012
    Posts:
    27
    Hi.

    How many tiles are you working with?. Maybe you can just disable the rendering component of the tile instead of its root GameObject.
     
  5. Narkata55

    Narkata55

    Joined:
    Feb 25, 2017
    Posts:
    63
    That would work, but it would make the 4-500 extra gameobjects active in the scene which i figured would be pretty bad for performance (even though they're just alternating sprites)
     
  6. Narkata55

    Narkata55

    Joined:
    Feb 25, 2017
    Posts:
    63
    Hmmm, ill try checking the profiler. Legitimately, the only thing these gameobjects are doing is switching alpha levels every ~0.3 seconds to make it seem like they're pulsing so I didn't think that would be much, if anything in onEnable
     
  7. Narkata55

    Narkata55

    Joined:
    Feb 25, 2017
    Posts:
    63
    Its 2 separate pools of about 200 tiles, but the only thing on them is a script that alternates their alpha level. Do you mean disabling just the sprite renderer? Do you think that would make a difference if thats the only thing on these gameObjects anyway?