I’m working on a multiplayer project which means many player related scripts can’t be referenced at design time. For that reason, I’ve had to make extensive use of GetComponent, GetComponent.Find, etc… and I was wondering whether executing say 20 of them in a foreach loop in a single go, while the player is staring at inventory screen, would be considered bad practice? This happens regularly, but as I say, all at once, during a menu.
No, there’s no problem doing this. In general when you can easily avoid getting the component every time you may want to cache them. GetComponent isn’t that bad as often advertised. Of course it has some overhead but so has setting the position of a gameobject.
What you should try to avoid (especially in weak environments like mobile or WebGL) is using “GetComponents” or “GetComponentsInChildren” as they return an array which will produce garbage.