Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Width is 0 after setting DisplayStyle to Flex.

Discussion in 'UI Toolkit' started by CoryMaklin, Sep 23, 2021.

  1. CoryMaklin

    CoryMaklin

    Joined:
    Oct 10, 2020
    Posts:
    29
    I have a "ghost" element that I'm using to simulate dragging an item. On the mouse down event, I want to center the element at the mouse position. Therefore, I do the following:

    Code (CSharp):
    1. m_GhostElement.style.display = DisplayStyle.Flex;
    2. m_GhostElement.style.left = evt.mousePosition.x - m_GhostElement.layout.width / 2;
    However, the width is 0 at that point of the code. Is there something I'm missing? Do I have to wait until the end of the frame for the display style change to take effect?

    The reason I say that, is the logic works in the subsequent mouse move event callback.
     
    Last edited: Sep 23, 2021
  2. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    733
    Yes, this change doesn't happen immediately.

    You could use the visibility=hidden/visible style instead which doesn't change the size of the element.
     
  3. CoryMaklin

    CoryMaklin

    Joined:
    Oct 10, 2020
    Posts:
    29
    Gotcha. Is there a way of setting the visibility to hidden and ignoring events?