Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Get UI components by name with GetComponent<>

Discussion in 'UGUI & TextMesh Pro' started by Reizla, May 31, 2016.

  1. Reizla

    Reizla

    Joined:
    Nov 5, 2013
    Posts:
    136
    I'm in the process of making a couple of UI elements with a dropdown where I've set the OKAY button as child of to the dropdown to read the current option from the dropdown by GetComponentInParent.
    Code (csharp):
    1. myDropdown=GetComponentInParent<Dropdown>();
    Now I'm adding 2 new UI elements (toggles) that the OKAY button must read as well. Sadly though a line like
    Code (csharp):
    1. myToggle1=GetComponentInParent<Toggle>();
    is not working. Instead I've opted to let the OKAY button and then use GetComponent<>.
    Code (csharp):
    1. Dropdown myDropdown;
    2. Toggle myToggle1, myToggle2;
    3.  
    4. this.transform.SetParent(GameObject.Find("Toggle1").transform);
    5. myToggle1=GetComponentInParent<Toggle>();
    6. this.transform.SetParent(GameObject.Find("Toggle2").transform);
    7. myToggle2=GetComponentInParent<Toggle>();
    8. this.transform.SetParent(GameObject.Find("Dropdown").transform);
    9. myDropdown=GetComponentInParent<Dropdown>();
    I figure that the above should be done easier with direct commands but have no clue how. So my question is, HOW (if possible) do I get a (UI) component using GameObject.find()?
     
    Last edited: May 31, 2016
    arhimenrius likes this.
  2. crispybeans

    crispybeans

    Joined:
    Apr 13, 2015
    Posts:
    210
    There are a couple of diffirent ways to get UI Compoents.

    1) GameObject.Find("aname").GetComponent<UICompoenntType>();

    2) Component.FindObjectOfType<UIComponentType>();

    3) Component.FindObjectsOfType<UIComponentType>().ToList().Find( x=>x.name == gameobjectName );

    These can be a bit slow and unreliable through. Especially if you have a big project and want to get inactive components as well.

    Just a suggestion, why don't you put scripts with the referances to the various components on the Widgets / View's you're developing ? Whats quicker and easier to resolve than using GameObject.Find and Component.FindObjectsOf...
     
    Taszty and Reizla like this.
  3. Reizla

    Reizla

    Joined:
    Nov 5, 2013
    Posts:
    136
    Thanks for the quick reply!

    For now I only need this method because I'm writing a plugin for Unity and use the UI elements only for demo purposes. I'll end up with about 8 prefabs and that'll be easy to keep track off AND the demo will be quickly enough to show the basic usage of the plugin.

    I have no clue about widgets at this moment, but I'll definitely will look into those when I get started on using the plugin into a bigger project...
     
  4. markroth8

    markroth8

    Joined:
    Mar 27, 2016
    Posts:
    1
    I've used something like this with success:

    Code (CSharp):
    1.     private T GetChildComponentByName<T>(string name) where T : Component {
    2.         foreach (T component in GetComponentsInChildren<T>(true)) {
    3.             if (component.gameObject.name == name) {
    4.                 return component;
    5.             }
    6.         }
    7.         return null;
    8.     }
    9.  
     
    Last edited: Feb 23, 2020
  5. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    Sorry to necro, Just wanted to update this with my solution in case anyone needs it, i did something similar with GetComponentsInChildren, i think this would be slightly more effecient(and easier to input) than the OP's solution
    Code (CSharp):
    1.  manFace = GetComponentsInChildren<SkinnedMeshRenderer>().ToList().Find(x => x.name.Contains("Head") );
    Basically, i had multiple skinned mesh renderers as a child of the object, i only wanted to get the head, but couldn't manually get it by sibling index(because it would sometimes be a different index) so i'm accessing it by name using the above lambda. Thanks to crispybeans for giving me the idea!