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 castor · Sep 28, 2012 at 12:32 AM · arraychildrenrange

GameObject add/remove children without causing errors.

My GUI for the game shows a thumbnail for the objects that the Player is carrying with him. Whenever a player is carrying an object they become his 'children'. For this I have a script on each object that loads a specific Texture2D.

My goal is that, when the player is not carrying anything it shows the default texture, otherwise it will read the script inside each of the children and show their respective thumbnail.

This is what I have so far:

 function OnGUI () {
    // GUI for Player Items  //    
         playerObjects = GameObject.Find("PLAYER").GetComponentsInChildren(Obj_Properties);
                 
         var imageWidth : int = 96;
         var frameDistance : int = 0;
         GUI.BeginGroup (Rect ((Screen.width * 0.5) - (totalFrameDistance * 0.5), 0, totalFrameDistance, 64 ), "");
                 GUI.Box (Rect (frameDistance,0,imageWidth,64), GUIContent("", PlayerObjSlot_00));
                     frameDistance += imageWidth +10;
                 GUI.Box (Rect (frameDistance,0,imageWidth,64), GUIContent("", PlayerObjSlot_01));
                     frameDistance += imageWidth +10;
                 GUI.Box (Rect (frameDistance,0,imageWidth,64), GUIContent("", PlayerObjSlot_02));
                     frameDistance += imageWidth +10;;
                 GUI.Box (Rect (frameDistance,0,imageWidth,64), GUIContent("", PlayerObjSlot_03));
                     frameDistance += imageWidth +10;
                 GUI.Box (Rect (frameDistance,0,imageWidth,64), GUIContent("", PlayerObjSlot_04));
                     frameDistance += imageWidth;
                     totalFrameDistance = frameDistance;
         GUI.EndGroup ();
         
         PlayerObjSlot_00 = playerObjects[0].GetComponent(Obj_Properties).objThumbnail;
         PlayerObjSlot_01 = playerObjects[1].GetComponent(Obj_Properties).objThumbnail;
         PlayerObjSlot_02 = nullTexture;
         PlayerObjSlot_03 = playerObjects[3].GetComponent(Obj_Properties).objThumbnail;
         PlayerObjSlot_04 = playerObjects[4].GetComponent(Obj_Properties).objThumbnail;


My current character has 2 objects and those show correctly but all the other 3 slots, since the children don't actually exist I get a 'Array index is out of range.'. I tried to prevent that by writing these lines but they dont seem to work. How do I write something that understand when the array children dont exist?

             if (playerObjects[3] != null){
                 PlayerObjSlot_03 = playerObjects[3].GetComponent(Obj_Properties).objThumbnail;
             }
             else {
                 PlayerObjSlot_03 = nullTexture;
             }
             if (playerObjects[4] != null){
                 PlayerObjSlot_04 = playerObjects[4].GetComponent(Obj_Properties).objThumbnail;
             }
             else {
                 PlayerObjSlot_04 = nullTexture;
             }

Hope the question is clear! Help is very welcome!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by kmeboe · Sep 28, 2012 at 12:44 AM

"Array index is out of range" means that you're trying to access elements of an array that don't exist. It's not that they're null, but rather that the array isn't that large. Try this modification of your second chunk of code:

     if ((playerObjects.Length > 3) && (playerObjects[3] != null)){
        PlayerObjSlot_03 = playerObjects[3].GetComponent(Obj_Properties).objThumbnail;
     }
     else {
        PlayerObjSlot_03 = nullTexture;
     }
     if ((playerObjects.Length > 4) && (playerObjects[4] != null)){
        PlayerObjSlot_04 = playerObjects[4].GetComponent(Obj_Properties).objThumbnail;
     }
     else {
        PlayerObjSlot_04 = nullTexture;
     }

Let us know how that works.

Comment
Add comment · Show 2 · 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 castor · Sep 28, 2012 at 04:12 PM 0
Share

worked like a charm! I also realized that now I can remove the 'playerObjects[4] != null' since it doesn`t seem to do anything. Thanks for the help!

avatar image kmeboe · Sep 28, 2012 at 04:53 PM 0
Share

Yeah, that makes sense.

Glad I could help.

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Find Child of GameObject from Array in Different Script 0 Answers

Random Material 0 Answers

Array out of its own range? 4 Answers

Array out of range 1 Answer

renderer.material doesnt work 3 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