Home > Mobile >  The object's height sets to 0 - Unity
The object's height sets to 0 - Unity

Time:10-21

I am a beginner in Unity and I am currently making a simple game. I recently solved my problem of scrolling two objects at the same time, but the vertical layout will put the other at the bottom. The answer came from the question I posted a while ago which is to use LayoutElement component. It does work in general, but when I start or play in the Unity it does not work at the start, I need to disable it and enable at runtime for it to work.

Just to add, I noticed that the ObjectiveItem object is 0 everytime I start or play the project. And the height will set to its children's height when I disable and enable the LayoutElement component.

I think the Content Size Fitter sets the height to 0. because when I mess with any height related settings in like when I uncheck the height value in Child Force Expand it will set the height to the height of its children same goes to the Child Force Expand of the Lines and Items objects. And like I mentioned in the start, when I disable and enable the LayoutElement in the ObjectiveItem object it will set the height of the children or even when setting the object itself inactive and active. The main problem is that the height is set to 0 at the start of the game.

enter image description here

This is the object (ObjectiveItem) that is set to the Content of the ScrollRect. The object already has Vertical Layout Group and Content Size Fitter to get the height of its children.

enter image description here

CodePudding user response:

If you want scroll to work on new items you need to have a VerticalLayoutGroup that also has a ContentSizeFitter component with preferred height chosen, that way new items will increase the height of the vertical layout and if added correctly to the scroll component it will scroll with any height.

CodePudding user response:

If you use this combination of a LayoutGroup ContentSizeFitter you should also enable the XYLayoutGroup.ControlChildSize and not use Force Expand and then your child objects should either have a ContentSizeFitter (if it is further LayoutGroups) themselves or have a LayoutElement with according settings.

I know this feels extremely clunky but this is the way I was told and worked so far.

  • Related