- Home /
The question is answered, right answer was accepted
Most performance friendly way to "unrender" a GameObject?
If I want to disable an object that is in an object pool system, which method would be the most performance friendly?
Use setactive(false)
Disable the renderers of it and its children
Set its transform to somewhere extremely far away.
Note that the object in question does not move at all, so the only reason for its existence is the renderer. All three methods work in this situation, but only because the object has a renderer and nothing else.
Answer by RLin · Jun 08, 2015 at 03:29 PM
I tested the three methods myself and found that setting the object transform was the most performance friendly. Like I said, all three are viable options because the object only has a renderer. Thank you to those who posted answers. Also, @crohr, sorry for my stubbornness, but it paid off because my game now runs much faster.
I just want to clarify for future readers, that the selected method of moving the transform may be the most optimized for RLin, but can have side effects if you are not careful, because the GameObject is still active in the scene. Not trying to discredit the chosen solution, but what is optimized for one game may not be the best solution for another game.
The reason that I did not like the answer of using setactive is that I had a noticeable drop in framerate when it was used, but not with the other two methods.
Even though it seems strange i have noticed the same thing, especially on android ( mobile in general ). Any kind of disabling the object has quite an impact on the framerate as the object has to be removed from the rendering queue.
I once made a Trackmania-style game for android (of course with track editor ^^) and the creating and destroying of the track parts took quite some time. So i build a transparent object pool, thinking it should boost the performance of adding / deleting parts. However the speed increase was just between 30% - 40%. However it caused hundreds of other problems since the object isn't destroyed, they keep references between the parts (took a while until i found the last reference). You really see strange things going on if there are dead references to diabled objects in a pool.
Finally i kept using the pool and used SetActive to disable them since it was already written and it gives you a slight boost.
$$anonymous$$eeping the objects active wasn't an option for me since the inpact on rendering performance was too much. This was mainly an issue for track loading and while using the track editor. It was more important to get a smooth racing game, even when the loading time is a bit longer ^^.
I gave you both an upvote. Especially: "what is optimized for one game may not be the best solution for another game". If optimising would be that easy, Unity would provide an general purpose "Optimise()" method ^^
Answer by Pascal97 · Jun 07, 2015 at 07:08 PM
I am now 100% sure but I wouldn't recommend 3) cause the obkect still is in the scene as before and it doesn't change that much. 2) would only deactivate all renderes whereas 1) would also set all colliders and other components to inactive. I so would take 1).
If you read above, the object doesn't have any colliders, and exists only for the renderer.
Answer by screenname_taken · Jun 08, 2015 at 03:21 PM
Use SetActive(false). That disables the object completely. So if you have something searching for stuff in the scene by name, Unity won't go over the disabled objects. If you just disable the renderer Unity will still check if it should do something with that depending on the rest of the scripts.
Try not to delete objects unless you really need to, as that will make garbage for the collector in the end.
Answer by kidne · Jun 08, 2015 at 05:53 AM
Set active to false! Because then it's also disabling renderers.
You could also the Destroy(this.GameObject) function if you won't be needing it anymore.
Answer by crohr · Jun 07, 2015 at 07:33 PM
I agree with Pascal, using SetActive(false) would be the preferred method for deactivating a GameObject for two reasons.
Using SetActive(false) will set the GameObject to be disabled and will disable all components on the GameObject.
SetActive(false) will also disable all of the child GameObjects.
If you read above, the object doesn't have any colliders, and exists only for the renderer.
I didn't mention anything about colliders, I mentioned other components, which could be anything including custom scripts, not just colliders. I stand by my answer which is using SetActive(false) is the most effective way of disabling a GameObject.
Sorry, I guess I wasn't specific enough. The object only has a mesh renderer, mesh filter, and transform. It does not have any scripts or other components. Because of this, all three of the options are viable for my situation, I just want to know which one will cause the least hiccups/lag.
You have three people now telling you that SetActive(false) is the way to go. I am not sure if you just don't like the answer or what. But the answer is without a doubt SetActive(false), the fact that you do or do not have additional scripts on the object doesn't change the answer.
Sorry about being stubborn, but after much testing I found that setactive is awful for performance. Setting the transform turned out to cause the least hiccups. I also changed the title and description to be more accurate.