0
\$\begingroup\$

I'm sure there is a very simple solution to this issue. I am creating a UI for a 2D RPG in Unity.

I have a Canvas with a two GameObjects:

DialogManager & MenuManager

The Dialog Manager has a child "DialogBox" which is a UI Image.

And the Menu Manager has three children for each part of the menu, so: three UI Images.

How the heck do I access these UI Images in a script attached to a different gameObject? Since they're technically not gameObjects, I can't just use the old gameObject.Find(). I've seen some things about Transforms and Children, but I just can't wrap my head around it.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

They aren't technically GameObjects, but are attached to GameObjects, and all GameObjects have by default a Transform. So, what you could do, in scripting, is to use a Transform's method called GetChild(index), receives an index of the child you want to retreive. Now, you may have the parents' references as Canvas, Image, etc., but all Components have a reference to their GameObject and Transform, so you can easily call that method:

public class Example : MonoBehaviour
{
    Image imageChild;

    void Awake()
    {
        imageChild = transform.GetChild(2).GetComponent<Image>(); /// Give me the third child, and give me its Image component
    }
}

If what you want is to retreive all the components in your children, then GetComponentsInChildren is what you are looking for:

public class Example : MonoBehaviour
{
    public Sprite spriteToAssign;

    void Awake()
    {
        Image[] childrenImages = GetComponentsInChildren<Image>();

        foreach(Image childImage in childrenImages)
        {
            childImage.sprite = spriteToAssign;
        }
    }
}

A way more easier and fast approach, would be to also have a public collection (array or list) of Images, and you assign them on the Inspector. As easy as this:

public class Example : MonoBehaviour
{
    public Image[] childImages;
    public Sprite spriteToAssign;

    void Start()
    {
        foreach(Image image in childImages)
        {
            image.sprite = spriteToAssign;
        }
    }
}

Hope it helps.

\$\endgroup\$
5
  • \$\begingroup\$ Thank you so much for your response, and taking the time to write out something to help a noobie like me. When I try to do: toSelect = menu.GetChild(2).GetComponent<Image>(); I get the error "GameObject does not contain a definition for 'GetChild' and no extension method 'GetChild' accepting a first object of 'GameObject' could be found." Am I missing a namespace? \$\endgroup\$ Commented Aug 10, 2018 at 1:15
  • 1
    \$\begingroup\$ No problem, don't forget to upvote the answer if it was useful to your problem. \$\endgroup\$ Commented Aug 10, 2018 at 1:17
  • \$\begingroup\$ Haha oops. Sorry, I think I posted my last comment posted before I was done. I've gone ahead and marked your answer as correct though. \$\endgroup\$ Commented Aug 10, 2018 at 1:27
  • 1
    \$\begingroup\$ No, that is actually one of the things I tried to point out: that method is a Transform's, so if you try to call it for another type of object (GameObject or Script) then do "menu.transform.GetChild(2);" \$\endgroup\$ Commented Aug 10, 2018 at 1:27
  • \$\begingroup\$ Ah, sorry I mis-typed. I was doing an uppercase Transform instead of lowercase. Thanks so much! \$\endgroup\$ Commented Aug 10, 2018 at 1:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .