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 08:25 PM · guidynamicchildren

Display buttons based on the amount of children

I want to show a GUI with a number of buttons depending on the amount of children the selected GameObject has. Each button should show a Texture2D image that is assigned as a variable via a script (Obj_Properties) inside each children. I`ve been trying to figure out how to do this but I don't think I have the knowledge to do so...

This is what I wrote so far for the GUIOn and it just doesn't seem to work:

     //number of children in Object.
     var numberOfItems : int = Player_Navigation.selectedObject.childCount;

     //number of children in Object that contain the Obj_Properties script (where I get the thumbnail image)
     var childrenInObject = Player_Navigation.selectedObject.GetComponentsInChildren(Obj_Properties);

     var yOffset  = 0;
     
     GUILayout.BeginArea(Rect (25,25,150,300));
     GUILayout.BeginVertical();
         for(i in childrenInObject){
             if( GUI.Button (Rect ( 30, 30 + yOffset, 30, 43), GUIContent("", childrenInObject[i].GetComponent(Obj_Properties).objThumbnail))){
                 Debug.Log("something :" + i );
             }
             yOffset += 30;
         }
     GUILayout.EndVertical();
     GUILayout.EndArea();

Any help 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 whydoidoit · Sep 28, 2012 at 08:29 PM

You are nearly there! Because you are using in the if line should read:

     if( GUI.Button (Rect ( 30, 30 + yOffset, 30, 43), GUIContent("", i.objThumbnail))){
    
Comment
Add comment · Show 8 · 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 whydoidoit · Sep 28, 2012 at 08:30 PM 0
Share

It might complain about objThumbnail not existing in JS depending on whether you have #pragma strict - if it does you can use (i as OBJ_Properties).objThumbnail

avatar image castor · Sep 28, 2012 at 08:35 PM 0
Share

Super fast reply and it works!! (I added the 'GetComponent(Obj_Properties)' so it doesn't complain.

avatar image castor · Sep 28, 2012 at 08:38 PM 0
Share

But now I realized that this way I also get the parent in the group, what would be the best way to ignore it from that list?

I have a boolean inside Obj_Properties that says if the object is parented or not. $$anonymous$$aybe its something I would use here?

avatar image whydoidoit · Sep 28, 2012 at 08:41 PM 0
Share

Easiest way would be an if in the loop.

 if(i != Player_Navigation.selectedObject)
 {
     //Loop body
 }
avatar image whydoidoit · Sep 28, 2012 at 08:43 PM 0
Share

Oh hell - you probably want the OBJ_Properties from that - damn.

 var parent = Player_Navigation.selectedObject.GetComponent(OBJ_Properties);
 
 for...
      if(i != parent)
Show more comments

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

Developing a Dynamic GUI 1 Answer

Is it possible to create a button dynamically? 1 Answer

Dynamically creating buttons with GUILayout.Button and sprites to textures 2 Answers

Create new GUI TextArea on click 1 Answer

french accent and dynamic fonts 1 Answer


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