# Creating a Bootstrap

### Overview

Creating a bootstrap provides a way to link a physical Unity scene to the framework's scene management system. Under the hood, all bootstraps should extend from `AbstractBootstrap`, which acts as the base class for integrating with the framework.

A bootstrap is still a regular `MonoBehaviour`, meaning it operates within Unity's GameObject and component system. This allows you to attach it to a GameObject in your scene and use familiar Unity features like `Start`, `Update`, and dependency injection provided by the framework.

### Dependency Injection

When using the `[InjectField]` attribute to inject dependencies, it’s important to note that these dependencies will only be resolved once the `OnBootstrapStartAsync` method is called. This ensures that the bootstrap has been fully initialized within the framework and that all required services are available. Attempting to access dependencies before this point may result in null references or incomplete setup.

### Canvases

Similarly, the `[FetchCanvas]` attribute allows you to automatically resolve and bind canvas objects to fields. Canvases marked with `[FetchCanvas]` will only be fetched and resolved after `OnBootstrapStartAsync` has been executed. This ensures that the bootstrap has discovered and initialized all active canvases in the scene.

### Best Practices

It is recommended to create a custom interface that implements `IBootstrap` for each bootstrap you define. This approach ensures better code organization and makes it easier to navigate to a scene programmatically using its specific interface. For example, instead of relying on generic methods, you can directly access scene-specific functionality through the strongly-typed interface. This practice enhances code readability, reduces the risk of errors, and provides clear separation between different scenes and their associated logic.

<pre class="language-csharp"><code class="lang-csharp">public class GameBootstrap: AbstractBootstrap 
{
    // Using our D.I you can inject your dependencies
    [InjectField] IPlayerController _playerController;
    
    // Automatically fetch the correct framework canvas reference
    [FetchCanvas] IGameHud _hud;
 
    public override async Task OnBootstrapStartAsync()
    {
        await base.OnBootstrapStartAsync();
        
        // Dependencies are resolved here
<strong>        
</strong>        await _hud.Show();
    }
   
    public override async Task OnBootstrapStopAsync()
    {
        // Stop any action before this scene is destroyed
        await _hud.Hide();
    
    
        await base.OnBootstrapStopAsync();
    }
    
    // Navigate to a game over scene
    private async void GameOver() 
    {
        await _navigationService.NavigateAsync&#x3C;IGameOverBootstrap>();
        await UnloadAsync();
    }
}
</code></pre>
