Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 UnitedStrength · Sep 20, 2015 at 06:44 PM · uilayouttroubleshooting

Question regarding VerticalLayout - list is not populating correctly except during Start()!

Hello! I am currently working on a project to make a dynamic inventory screen. The inventory needs to populate a list of items at runtime based on clicking a tab for that type of item (weapons, armor, etc.) I am using a ScrollView with a vertical layout to fit the items as described in the tutorial here: https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/creating-scroll-lists-at-run-time

I followed it and everything worked great! I use the following function to populate the list

 void PopulateList(List<Item> items)
     {
         foreach(Item item in items)
         {
             // Create the list item in gamespace
             GameObject list_item = Instantiate(inventory_list_item) as GameObject;
             list_item.transform.SetParent(contentPanel.transform, false); // child it to the panel object that we will populate the ScrollView with.
             List_Item itemScript = list_item.GetComponent<List_Item>();
             // Send it a reference to the item that it represents
             itemScript.BuildListItem(item);
 
             // We want the item to register click events.
             list_item.GetComponent<Button>().onClick.AddListener(delegate { InvItemIsClicked(itemScript); });
         }
         inventoryUpdated = false;
     }

So the problem happens as follows. When I use Start() to call this method and pass it a list, it works fine: alt text

However, if I call this function by clicking a button, it sticks all of the elements at one spot. For example, after clicking the junk items button: alt text

I am not sure exactly what is going wrong here, since the list is the same and the method being called is the same. Is a VerticalLayoutGroup only set up during Start(), or is there something else I need to do?

Thanks for taking a look at this!

Comment
Add comment · Show 1
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 UnitedStrength · Sep 18, 2015 at 01:40 PM 0
Share

So after hours of playing around with my code and looking at forums, it looks like I've found the answer to this question. You have to call

LayoutRebuilder.ForceRebuildLayoutImmediate([your content panel here].transform asRectTransform);

after you modify what's in a list. This fixes this problem! http://forum.unity3d.com/threads/dynamically-added-ui-doesnt-get-affected-by-layout-groups-and-content-size-fitters.355279/

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

uGUI Vertical Layout Group stretching objects 2 Answers

Horizontal Layout Group padding update via script 3 Answers

how to extend the GUILayout scrollview's function? 0 Answers

What is the LayoutElement.FlexibleWidth/FlexibleHeight above 1 for? 0 Answers

How do I code a custom LayoutGroup? 0 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