Search Unity

Finding X objects dynamically in prefab mode?

Discussion in 'General Discussion' started by Mike01923, Sep 24, 2021.

  1. Mike01923

    Mike01923

    Joined:
    Jun 19, 2015
    Posts:
    195
    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?
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,571
    FindObjectsInChildren.

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

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,982
    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
     
    neginfinity likes this.
  4. ExtraCat

    ExtraCat

    Joined:
    Aug 30, 2019
    Posts:
    52
    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.
     
  5. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,571
    angrypenguin likes this.