Game Objects with multiple children taking time to SetActive initially
Hi guys , Basically i have a game object "Dictionary" which has multiple prefabs (Roughly 500) which is instantiated during runtime and is disabled initially while enabling it with a button click it takes a lot of time and unity freezes during this process iam using SetActive(true) anitially and on button click SetActive(False)
any idea on y this is happening???
Thanks
Do you setActive() the father object only? or all of the 500? $$anonymous$$aybe put some code, so we can help !
bdw, I'm pretty sure there muste be an other way to do your dictionnary stuff than instantiating 500 objects.
thank you for your reply
yes i setactive only the parent which has around 400-500 text prefabs as children
start(){ Dictionary.SetActive(true) }
Buttonclick(){ Dcitionary.SetActive(false) }
Thank you
Answer by Blue-Cut · Jun 08, 2016 at 11:32 AM
Hello,
I am not sure if I understand the problem because your very long sentence is not totaly clear to me. What I understand is that you want to instanciate 500 prefabs at runtime and the freeze happens at this moment. My answer corresponds to this interpretation :
The freeze sounds normal as you have 500 objects to handle inside the unique frame when you click the button. The problem is that the loop on the 500 objects is synchronous, it's a group of instructions that execute during the frame when the button is clicked.
A thing you could do is writing a coroutine that splits the process on several frames. And while it's not over, you can continue your game or display a loader depending on the behaviour you want.
In this way, there will be no "freeze" to give a negative effect.
Hi thanks for the reply but during instanciatin the prefab unity does not freeze Say A gameobject "dictionary" has 500 childrens which are instantiated in the begining in start this works fine
the problem comes when on a button click Example: void click() { dictionary.SetActive(true); } i try to Activate the game object with 500 children it freezes/hangs for about 15-20 seconds
Thanks
Answer by Morgenstern_1 · Jun 08, 2016 at 03:56 PM
Regardless of how the objects are parented you're still enabling/disabling 500 objects at a time. That will take a while, especially if the child objects contain scripts which use OnEnable/OnDisable.
Is there a way to break down your structure into more manageable groups so that you don't need to disable/enable as much at once?
Hi thanku for the reply
what i have is a dictionary gameobject in which around 400- 500 text prefabs are instantiated in a form of accordion in the start (no delay o lag during this process) all these prefabs are the child of the dictionary
I just used start(){ dictionary.SetActive(false); }
ButtonClick(){ dictionary.SetActive(true); }
the lag/freeze happens only for the first time i open the dictionary is there any other method on activating a game object on click???
thankyou
If it's only happening the first time you enable all the objects then perhaps look at the code you have in any Awake() or Start() functions on those objects. Perhaps there's something expensive happening in there that could be done another way.
Have you tried running the profiler?