Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Redden44 · Dec 13, 2014 at 01:08 PM · childboundsoutof

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 :)

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ahmedbenlakhdhar · Dec 13, 2014 at 01:30 PM 0
Share

To which game object this script is attached?

avatar image Redden44 · Dec 13, 2014 at 03:59 PM 0
Share

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

avatar image Redden44 · Dec 13, 2014 at 04:05 PM 0
Share

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 :)

avatar image SkaredCreations · Dec 13, 2014 at 04:07 PM 0
Share

[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.

avatar image Redden44 · Dec 13, 2014 at 04:11 PM 0
Share

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 :)

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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".

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Redden44 · Dec 13, 2014 at 04:05 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges