Reputation: 1815
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:
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;
}
Upvotes: 0
Views: 1640
Reputation: 1044
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.
Upvotes: 0
Reputation: 13297
Are you sure that your Image game object is active? GetComponentsInChildren
has includeInactive
parameter, and it's set to false
by default.
Upvotes: 1
Reputation: 95
Maybe you should consider including "Panel" in your resource loading path.
Upvotes: 0