Unity UiElement created at runtime is not displayed

Viewed 16

I created a uielement and gameobject in the runtime and set it to follow the world position of the gameObject.

but the uielement is not displayed.

I've tried a few things and found a way to display them.

public void LateUpdate()
{
    var position = gameObject.transform.position;
    var newPosition = RuntimePanelUtils.CameraTransformWorldToPanel(gage.panel, position, Camera.main);
 
    gage.transform.position = new Vector2(
        newPosition.x - gage.layout.width * 0.5f,
        newPosition.y
    );
}
  1. Change the LateUpdate function to Update and it will be displayed.
  2. Select Element from the UI toolkit debugger and change the style and it will be displayed.
  3. Change the element style elsewhere than the function that generated the uielement at runtime and it will be displayed.
  4. Following objects already created in the scene works correctly.

Why is this happening? How do I display elements correctly?

0 Answers
Related