Finding X objects dynamically in prefab mode?

For my UI layout, I have containers with bright images that help me understand my layout better. I have a toggle where I can turn them off and on with OnValidate, the only problem is that I need to manually drag each one into an array, which gets annoying since I often add / remove containers. I’ve tried to attach a script to each container and use FindObjectsOfType, but that doesn’t work in prefab mode. Is there a way to dynamically find my containers in prefab mode so I can switch them on and off while also in prefab mode?

FindObjectsInChildren.

You can also manually find things by iterating through Transform.children recursively.

Thats not part of vanilla Unity API is it? It must be coming from some sort of extension you have because searching for it finds nothing in unity docs

1 Like

I’m pretty sure there is only “GetComponentsInChildren”, yeah.

And I guess OP only needs [ExecuteInEditMode] attribute in his script to be able to find objects in prefab mode too. With proper limitations to avoid performance issues of course.

Alright that was a mistake on my part.

“GetComponentsInChildren”
Same deal.
https://docs.unity3d.com/ScriptReference/Component.GetComponentsInChildren.html

1 Like