- Home /
Transform child out of bounds
I'm getting this error: UnityException: Transform child out of bounds
I google it and it means I'm trying to access a child which doesn't exist, eventhough my child does exist.
I'm trying to access to a text which is the 2nd children of an image: [img]http://i.imgur.com/8HNPbvD.jpg[/img]
The line with the error is this one inside the "else if" statement:
Text text = child.GetChild(1).GetComponent();
Here's the code:
void RefreshInventory()
{
int i = 0;
foreach (Transform child in transform)
{
slotsList[i] = inventoryList[i];
if (slotsList[i].itemName != null)
{
Image image = child.GetChild(0).GetComponent<Image>();
image.enabled = true;
image.sprite = slotsList[i].itemIcon;
if (slotsList[i].itemStackable == true && slotsList[i].itemStack > 1
{
Text text = child.GetChild(1).GetComponent<Text>();
text.enabled = true;
text.text = slotsList[i].itemStack.ToString();
}
}
else if (slotsList[i].itemName == null)
{
Image image = child.GetChild(0).GetComponent<Image>();
image.enabled = false;
image.sprite = null;
Text text = child.GetChild(1).GetComponent<Text>();
text.enabled = false;
}
i++;
}
}
Thanks :)
This lines are part of the script Inventory which is attached to the inventory GameObject which is a panel, you can see it in the screenshot I posted.
The first child of the inventory is a image called slot and I use a loop to pass all the childrens and for every child I check their children with GetChild(index); the first child is always the image, so index 0, the second child is always the text, so child index 1. The script worked fine wich just the part with the image and GetChild(0), the I added the part with the text and GetChild(1) and it broke :p
This is part of the script Inventory which is attached to the GameObject inventory.
The part about the image worked fine, then I added the part about the text and it gave me the error. GetChild(0) works fine, GetChild(1) gives the error, and I don't understand why :(
Does FindChild use more resources than GetChild(index)? Thanks :)
[OT]: Please remember to use "add new comment" ins$$anonymous$$d of "post your answer".
Well, if the script is attached to Inventory then make sure that all the "Slot*" objects have both Image and Text, probably you missed to add both these objects to one of them.
Sorry about that, I realized it too late.
And you were right I forgot just 1 single text out of 30, which messed up the whole script, feeling dumb right now hehe
Thank you very much :)
Edit: Last question, Does FindChild use more resources than GetChild(index)? Thanks :)
Answer by SkaredCreations · Dec 13, 2014 at 02:11 PM
This script works only if it's attached to the game object "Inventory", anyway I would use FindChild("Image") and FindChild("Text") instead of GetChild because so it'll work even if the first child is "Text" instead of "Image".
This lines are part of the script Inventory which is attached to the inventory GameObject which is a panel, you can see it in the screenshot I posted.
The first child of the inventory is a image called slot and I use a loop to pass all the childrens and for every child I check their children with GetChild(index); the first child is always the image, so index 0, the second child is always the text, so child index 1. The script worked fine wich just the part with the image and GetChild(0), the I added the part with the text and GetChild(1) and it broke :p
Does FindChild use more resources than GetChild(index)? Thanks :)
Your answer
Follow this Question
Related Questions
Why is this array out of length? 1 Answer
Issue using separate GameObject to detect bounds 0 Answers
IndexOutOfRangeException 1 Answer
Wierd Animation Bug 0 Answers
C# - IndexOutOfRangeException - Array index is out of range 2 Answers