remove value from LayoutElement.preferredWidth by script

Hey guys,

I spent quite some time trying to find out myself and searching the net, without any success, so I really need your help…

Under a UI panel with a HorizontalLayoutGroup I have some child components, of which I need to control the width programmatically in certain situations.
To achieve this, I have a LayoutElement attached to the children and set the PreferredWidth by script to the required value, which is working fine.

Unfortunately, there are also situations where I need to remove the preferred width, so that the element width is again under full control of the parent’s LayoutGroup.
How can I achieve this? I couldn’t find any method, nor does the float property accept null as value.

Any help is appreciated!

Lol, it seems I just found the solution myself:

Before, I was only trying to set the value to 0, but setting the value to -1 finally did the trick.

Hope this helps someone in the future who has the same problem.
And maybe the according information will find its way into the LayoutElement documentation as well… :wink: