Search Unity

HorizontalLayoutGroup or VerticalLayoutGroup not updating when I add new child

Discussion in 'UGUI & TextMesh Pro' started by Culzean, Mar 29, 2016.

  1. Culzean

    Culzean

    Joined:
    Jan 25, 2014
    Posts:
    48
    I am setting placing a new gameobject as a child of the layout group like so,

    this.transform.SetParent(_parent);

    where this is the child object and parent is the transform of the layout group. Previously this had updated, I think the layout group must have had the dirty flag set.
    However this had stopped working. Now the child object is added correctly to the parent, but it's position is not updated in accordance with the layout group.

    How do I prompt HorizontalLayoutGroup that there is a new child and it should update the position of all child objects?
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
  3. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    What do you mean it worked previously? In a different version of unity? If so could you please file a bug report and post the number here, please include an example project to help speed things up :)
     
  4. Culzean

    Culzean

    Joined:
    Jan 25, 2014
    Posts:
    48
    Yup I do mean in a previous version of Unity. I would need to look through commit logs in order to figure out the exact version. I'll get a demo scene together, maybe tomorrow or the next day.

    @eses I have not seen this component before, thanks I'll take a look :)
     
    karl_jones likes this.
  5. Zdzislaw

    Zdzislaw

    Joined:
    Sep 20, 2012
    Posts:
    19
    I have similar problem with general GridLayout (exists in 5.3.4 and 5.4 didn't check previous versions) - reproducible in a new scene with just canvas+panel with grid layout group. There are two cases (I suppose it's the same issue):
    1) like OP wrote adding element through code doesn't set proper position in panel
    2) adding elements manually in the editor doesn't set proper position for the first element until I duplicate existing element:
    - after adding first element:
    1.png
    - after adding second element:
    2.png
    - after duplicating second element:
    3.png

    Conclusion: duplicating element in a panel enforces recomputing of positions of all elements.

    Bonus question: can I somehow enforce such recompute from script?

    Found using Uncle G, but doesn't work for me:
    - transform.SetParent(transform, false);
    - transform.localScale = Vector3.one;
    - LayoutRebuilder.MarkLayoutForRebuild (transform as RectTransform);
     
    Laulaurene and Culzean like this.
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    Can you file a bug report with that example included please.
     
  7. Zdzislaw

    Zdzislaw

    Joined:
    Sep 20, 2012
    Posts:
    19
    Done.
     
    karl_jones likes this.
  8. Nyankoooo

    Nyankoooo

    Joined:
    May 21, 2016
    Posts:
    144
    Any update on this as I'm getting this still in 5.5.0f3?
     
  9. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    I can't see any active bugs for this. Do you have a bug number?
     
  10. MrNowak

    MrNowak

    Joined:
    Nov 26, 2015
    Posts:
    3
    Has this been fixed. Ran in to a similar issue.
     
    rnnicoletti and Laulaurene like this.
  11. shazamsoyeux

    shazamsoyeux

    Joined:
    Jun 14, 2016
    Posts:
    3
    I had the same issue by using SetParent().
    Code (CSharp):
    1. GameObject newGO = Instantiate(goPrefab);
    2. newGo.SetParent(goParent); // doesn't update layout
    To fix it instantiate de newGameObject directly inside the parent.
    Code (CSharp):
    1. GameObject newGO = Instantiate(goPrefab,goParent); // updates layout
     
  12. yetimade

    yetimade

    Joined:
    Dec 5, 2022
    Posts:
    1
    This component not working; tried it across a few different editors and files ‍♀️
     
  13. rodrigomp6

    rodrigomp6

    Joined:
    Apr 22, 2021
    Posts:
    1
    Still not working...