Operation lifetime11 / 13

Async operations and scene exit

Check whether the component is still enabled after loading.

Commands use awaitSceneScope for this check. In a component, use awaitComponentScopeResource when the loaded resource needs to be released.

const spawned = await this.awaitComponentScopeResource(
  this.node.spawnPrefab({
    prefabId: this.config.prefabId,
    localId: 'preview',
  }),
  (resource) => resource.dispose(),
);
if (!spawned) return;
spawned.node.setVisible(true);

If the component has been disabled, the method releases the prefab and returns undefined.

Owner is activeOperation completes → use the result
Owner has been removedOperation completes → release the resource → skip the rest
The check after await lets the network request finish and controls whether its result is used.

Call super.onDetach() in your onDetach method so the SDK stops animations registered through ownTween.