- Home /
200 FindChild calls for big UI bad practice?
I've got a prefab (my main UI) with somewhere around 100 or more public gameobjects assigned to lots of different scripts. One time I had a bug detach all those and it was a big waste of time finding them all and reattaching them. I was wondering if it would be horribly bad practice to program them all to find their children via script and never actually attach anything in the inspector.
I already try to avoid inspector use as much as possible... Like storing an array in the inspector, just seems unsafe...
The bigger the UI gets the more I realize i should break it down in to parts and that just makes moving the spawning and control to script make that much more sensible me thinks...
Would calling say 200 findChild commands once per game (at start up) be such a bother? Even if it took a couple frames it'd only be at the start... Does anyone else program like this/have experience trying this? I might program everything from here on if that way unless someone has a reason why not to.
Cheers.
In my experience, things which only occur once hardly matter. If it has to cycle over and over, then yeah, it can become problematic.
Answer by Fattie · Apr 17, 2015 at 03:00 AM
You have it wrong, it is
BETTER PRACTICE
to attach them by script!
"Would calling say 200 findChild commands once per game (at start up) be such a bother"
that is exactly and precisely what you should do.
(BTW it would not take "a couple frames" - it would happen within one frame, but, it would take only a tiny amount of time.)
"Does anyone else program like this/have experience trying this?"
It is the only way you do professional engineering with unity!
"I might program everything from here on..."
if you want to ever say get paid to be a Unity engineer, you will in fact have to do it the way you describe! :)
Regarding the silly sort of "drag to connect" feature in the editor, it's just kind of a tool for beginners, for demonstration purposes, and for some special cases.
For 200 links adding them via a script is definitely more efficient. Especially with an efficient loop. Five lines to connect everything. I would have the script connect them up in edit mode rather then in the game, but that's just me.
sure, that's another issue. but in general it's funny to see that the OP is worried if it is "ok" to hook stuff up properly in code :)
cool deal. I'll get to changing it all some day then and code anything new from here on.
my concern was co$$anonymous$$g from people suggesting to not use GameObject.find on a regular basis, but thinking about it that is probably because some people are using it in Update, and are looking through everything in the scene...
thanks again for the clarification
those people are idiots. of course you don't use it IN A LOOP, DURING GA$$anonymous$$E PLAY (just as you correctly say).
Answer by Gf15a4 · Apr 17, 2015 at 02:52 AM
If you really want to reduce the wait time you would put all the UI under the canvas like its supposed to be. Then in your script you would do something like this.
link canvas into canvas variable
private Transform canvas;
void Update()
{
//canvas.GetChild(index under child at which its stored at).whateveryouwanttodotoit
/*below it grabs the first child under canvas at which it starts at 0. Then its position is randomized*/
canvas.GetChild(0).transform.position = new Vector3(Random.Range(0, 50f), 0, Random.Range(0, 50f));
}