3

Prefab hierarchy:

Button

1 Text

2 Image

I wish to access Image child component of the prefab.

I tried the following code:

GameObject addTypeButton = (GameObject)Instantiate(prefabButton);
addTypeButton.transform.SetParent(ParentPanel, false);
//set text
addTypeButton.GetComponentInChildren<Text>().text ="Some string";
//get image
WWW www = new WWW("someImagelink");
yield return www;
//set image
addTypeButton.GetComponentInChildren<Image>().sprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width,www.texture.height), new Vector2(0, 0));

However, the above code is accessing Image script of Button (inbuilt). Not the Image UI component. How do I access UI Image (child) component?

Please help.

Thanks!

3 Answers 3

16

The Cause

GetComponentInChildren will also return component on the gameObject itself.

public Component GetComponentInChildren(Type t);

Returns the component of Type type in the GameObject or any of its children using depth first search.

Solutions

  1. If the index of child GameObject 1 Text and 2 Image is fixed. You can get them by Transform.GetChild(index).

    var buttonTransform = addTypeButton.transform;
    var text = buttonTransform.GetChild(0);
    var image = buttonTransform.GetChild(1);
    
  2. If the order is not fixed, use Transform.Find(childName).

    var buttonTransform = addTypeButton.transform;
    var text = buttonTransform.Find("1 Text");
    var image = buttonTransform.Find("2 Image");
    
  3. The safest solution:

    Drag your prefab to scene and attach a script to your Button GameObject:

    using UnityEngine;
    using UnityEngine.UI;
    public class MyButton : MonoBehaviour
    {
        public Text text;
        public Image image;
    }
    

    Then drag 1 Text and 2 Image to text and image field in the inspector of the Button. how
    Remember to press apply button and Ctrl+S to save that into your prefab.

    In this way you can access the text and image like:

    var mybutton = addTypeButton.GetComponent<MyButton>();
    mybutton.text.text = "Some string";
    mybutton.image.sprite = Sprite.Create(...);
    
2
  • 3 answers but only this one will actually do the job. GetChild is better thanFind.
    – Programmer
    Commented Apr 9, 2017 at 18:40
  • I'd add that relying on objects names or hierarchy order may not always be the safest solution. For a more robust one you could use GetComponentsInChildren<Image>() and check if the return _Image_s are different than the one on your button (this however will be slower).
    – Kardux
    Commented Apr 10, 2017 at 8:58
0

Before accessing the child object, please make sure that that child object is enabled in prefab before enabling or disabling it after getting instantiated. Like in my case, I had a UI Text game object with an image child object. I disabled it in the prefab with the hope to enable it by reference in the script after instantiating it. It only worked if I kept the image(child) component enabled in the prefab.

-1

Try changing

addTypeButton.GetComponentInChildren<Image>().sprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width,www.texture.height), new Vector2(0, 0));

To

addTypeButton.GetComponent<Image>().sprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width,www.texture.height), new Vector2(0, 0));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.