- Home /
Alternatives to GameObject.SetActive(...)
So I have recently come across an issue activating/deactivating game objects, its become to slow of an operation. More specifically, whenever one of my spawn triggers are executed the game pauses for a brief moment as the target gameobject is enabled. I have taken a better part of the day to try and find a work around to this issue, but haven't had much luck.
I'm not doing any instantiation, this is a pure .SetActive(true) call on a game object and I've narrowed it down to being specifically that being the cause. My ideal solution would be to somehow toss the SetActive stuff onto a new thread, but I'm not even sure that's possible, or how to do it if it was.
Curious if anyone has any good alternatives to SetActive()?
I'm seeing this as well - does anyone know if that it is normal for SetActive to hang/pause the game for a frame?
Answer by Brian-Kehrer · Apr 15, 2013 at 04:58 PM
Have you deep profiled this? Calling SetActive should not be an expensive operation. More likely, something is happening in Start() that is expensive, and causing your aforementioned issue.
Of course, depending on what you actually want to disable, there are plenty of alternatives to turning off the gameObjects, such as disabling the individual components. What is it you are actually trying to accomplish, and what components are attached to the object? These are generally the causes of slow activation.
I've investigated further and deter$$anonymous$$ed that I'm getting the same issue as http://answers.unity3d.com/questions/210896/rendertexturesetactive-huge-profiler-spikes.html?page=1&pageSize=5&sort=votes. If I set to render maximized the problem goes away - but that's not really an answer to this issue. Additionally I'm not seeing the spike when I SetActive(false) on a general gameobject with mesh-based children, but I am seeing when I SetActive(false) on the mesh-based gameObjects themselves.
Answer by chmodseven · Jun 08, 2015 at 11:00 AM
I am also encountering a weird variant of this.
When I initialise my scene, I am using GetComponentsInChildren to loop through and find all UGUI child game objects containing a particular component, so I can cache them in a dictionary for faster access later. The search starts at each canvas root parent in the scene, and all child objects are active. The weirdness comes in with the root parent being enabled or not.
Now, if the canvas object is enabled, no problem - it all goes nice and fast. But if I set the canvas disabled in the scene (which is preferable, to avoid flickering) then GetComponentsInChildren runs very slow and adds up to 5 seconds to startup. No problem, you say, just SetActive it before you start getting the components: but when I tried that, the slowness doesn't go away even though effectively the objects are now in the exact same state as if I'd saved them as active in the scene. Weird.
Currently I am getting around it all by leaving the parents active but turning the camera off until I've done all the caching, to avoid the flickering. But I would much prefer to understand why this is happening like this, and why the mere presence of a disabled parent makes GetComponentsInChildren so considerably slow in the first place.
Your answer
Follow this Question
Related Questions
Calling setActive(false) on a disabled object? 3 Answers
How to disable an array of projectors 2 Answers
Help With Disabling/Enabling single GameObject 1 Answer
Enable/Disable Game Object With GUI Button 1 Answer
how to perform undo action in unity? 2 Answers