0

I am trying to alter the sprite of a nested image object within a dialog UI (Unity 4.6's UI) that I have saved as a prefab. I am able to load the prefab, but when I try to do:

Image[] imageComponents = conversationDialogNoChoice.GetComponentsInChildren<Image>();

I get zero items back. The hierarchy is:

enter image description here

The full code is:

        private GameObject conversationDialogNoChoice;
public void StartConversation(Conversation conversation)
    {
        if (!talking)
        {
            //StartCoroutine(DisplayConversation(conversation));
            StartCoroutine(DisplayConversation(conversation));
        }
    }

    IEnumerator DisplayConversationNewUI(Conversation conversation)
    {
        conversationDialogNoChoice = (GameObject)Resources.Load("Prefabs/ConversationDialogNoChoices");
        conversationDialogChoice = (GameObject)Resources.Load("Prefabs/ConversationDialogChoices");
        ConversationTest = (GameObject)Resources.Load("Prefabs/ConversationTest");
        bool nextPushed;

        foreach (var conversationLine in conversation.ConversationLines)
        {
            nextPushed = false;
            Image[] imageComponents = conversationDialogNoChoice.GetComponentsInChildren<Image>();
            Debug.Log(imageComponents.Length);
            //imageComponents[].sprite = currentCovnersationLine.DisplayPicture;

            Instantiate(conversationDialogNoChoice);

            while (!nextPushed)
            {
                if (Input.GetKeyDown(KeyCode.Return))
                {
                    nextPushed = true;
                }
                yield return null;
            }
        }

        talking = false; //talking is complete
        if (conversation.Repeatable == false)
        {
            conversation.CanOccur = false;
        }
        yield return null;


    }
Yecats
  • 1,715
  • 5
  • 26
  • 40

3 Answers3

1

Are you sure that your Image game object is active? GetComponentsInChildren has includeInactive parameter, and it's set to false by default.

Max Yankov
  • 12,551
  • 12
  • 67
  • 135
  • I have a GameObject obj: obj > Canvas > ImageMask > Image > text > Image(1) > image(2). There is a "nested" or "layering" of images in one GameObject. Say I want to access just the second image, "image (1)"-of-3 images. And with this image, turn it on and off so that the other images appear. Rather than load/unload images, turning off images would seem more efficient, that is if you were wondering, why do this? In Flash it was as ease as "Frame(2) turn off". – ejbytes Dec 23 '20 at 00:12
0

Maybe you should consider including "Panel" in your resource loading path.

0

In addition to what "Max Yankov" said.

Image in GetComponentsInChildren<Image>() refers to a script or component on the GameObject not the GameObject itself.

So if you need to find all Components of Image you need to add a Script or Component with name Image on your child GameObject. Naming the GameObject "Image" is not necessary.

Anas iqbal
  • 1,036
  • 8
  • 23