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
1
Question by Sendatsu_Yoshimitsu · Apr 06, 2017 at 11:42 AM · uicanvasgridlayout

Why doesn't childing a gameobject to GridLayoutGroup at runtime align it properly?

I'm trying to instantiate buttons inside of a grid layout group at runtime, and my assumption is that object.SetParent(grid,false) will automatically position each button with the correct cell spacing:

 public GameObject buttonPrefab;
 
 public override void MenuEnabled()
 {
     GridLayoutGroup myGridLayoutGroup = GetComponentInChildren<GridLayoutGroup>();
 
     for (int i = 0; i < 5; i++)
     {
         GameObject tempObj = Instantiate(buttonPrefab, Vector3.zero, Quaternion.identity);            
         tempObj.transform.SetParent(myGridLayoutGroup.transform, false);                       
     }
     
 }

This all looks fine to me, and these objects do spawn and child themselves to my grid, but they don't align to its cells: every single button's local transform remains at vector3.zero. A quick google seems to show everyone else doing it the same way I am, is there some kind of initialization I'm missing which instructs the grid layout group to properly orient its new child?

Comment
Add comment · Show 3
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 kaplica · Apr 06, 2017 at 02:16 PM 0
Share

I had similar issue, the thing that fixed it is to update a property like padding bottom to 1 and then back to 0. That would reset the grid and it's items to layout again.

avatar image Sendatsu_Yoshimitsu kaplica · Apr 06, 2017 at 03:51 PM 0
Share

That's so strange... changing the padding itself doesn't seem to work, you'd think if this were an intended part of the workflow, https://docs.unity3d.com/ScriptReference/UI.GridLayoutGroup.html would have some kind of public function to force the layout to update.

avatar image JustinEllis · Apr 06, 2017 at 04:56 PM 0
Share

Well I posted an answer, it just needs to await moderation...

1 Reply

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

Answer by JustinEllis · Apr 06, 2017 at 07:24 PM

Well I currently have a grid layout and im also instantiating at runtime. This is what I have done:

alt text

My code looks like this:

 GameObject NewFood = (GameObject)Instantiate(MainControl.instance.FoodItemPrefab, FoodItemsScrollView.transform, false);

The scrollview transform is the GameObject shown in the picture


capture.png (35.1 kB)
Comment
Add comment · 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

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

108 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need some help regarding UI layout in different Screen resolutions 1 Answer

Making Selectable UI Grid objects 0 Answers

Grid layout group stretching text 1 Answer

Unity UI - layout fixed number of buttons 1 Answer

Create placeholders in grid layout group? 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