CancellationToken EnabledToken { get; set; }

robot_2Generated
code_blocksInput

Description

This property provides a CancellationToken that is cancelled when the GameObject ceases to exist or is disabled. This can be useful for managing asynchronous operations that should be terminated when the GameObject is no longer active or valid.

Usage

Use the EnabledToken property to obtain a CancellationToken that will be automatically cancelled when the GameObject is disabled or destroyed. This is particularly useful for cancelling ongoing tasks or operations that are associated with the lifecycle of the GameObject.

Example

// Example of using EnabledToken to cancel an asynchronous operation
public async Task PerformOperationAsync(GameObject gameObject)
{
    try
    {
        // Pass the EnabledToken to the asynchronous operation
        await SomeAsyncOperation(gameObject.EnabledToken);
    }
    catch (OperationCanceledException)
    {
        // Handle the cancellation
        Console.WriteLine("Operation was cancelled because the GameObject was disabled or destroyed.");
    }
}

private async Task SomeAsyncOperation(CancellationToken cancellationToken)
{
    // Simulate a long-running operation
    await Task.Delay(5000, cancellationToken);
}