Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    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
  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...
     
    Reizla likes 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
    mastaqat likes this.
unityunity