diff --git a/cocos/ui/widget-manager.ts b/cocos/ui/widget-manager.ts index cb4085f597..0ff9b10305 100644 --- a/cocos/ui/widget-manager.ts +++ b/cocos/ui/widget-manager.ts @@ -204,7 +204,7 @@ function align (node: Node, widget: Widget): void { // TODO: type is hack, Change to the type actually used (Node or BaseNode) when BaseNode complete function visitNode (node: any): void { const widget = node.getComponent(Widget); - if (widget && widget.enabled) { + if (widget && widget.enabled && cclegacy.isValid(widget, true)) { if (DEV) { widget._validateTargetInDEV(); } diff --git a/cocos/ui/widget.ts b/cocos/ui/widget.ts index 44727d2af1..532a33f5cb 100644 --- a/cocos/ui/widget.ts +++ b/cocos/ui/widget.ts @@ -852,6 +852,9 @@ export class Widget extends Component { } public onDestroy (): void { + // `onDisable` is skipped when the component was never enabled by the scheduler + // (`IsOnEnableCalled` unset), so unregister here as well. + cclegacy._widgetManager?.remove(this); this._removeParentEvent(); }