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.

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! :slight_smile:

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.

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));
}