# How to make a button reload the scene, the button starts out as disabled until game ends

**URL:** <https://forum.needle.tools/t/how-to-make-a-button-reload-the-scene-the-button-starts-out-as-disabled-until-game-ends/1881>\
**Category:** 🌵 Needle Engine\
**Tags:** question, docs\
**Created:** [October 5, 2024, 11:01pm UTC](https://forum.needle.tools/t/how-to-make-a-button-reload-the-scene-the-button-starts-out-as-disabled-until-game-ends/1881 "2024-10-05T23:01:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nick\_Romanek](https://forum.needle.tools/user_avatar/forum.needle.tools/nick_romanek/32/3583_2.png) [@Nick\_Romanek](https://forum.needle.tools/u/Nick_Romanek)\
**Post date:** [October 5, 2024, 11:01pm UTC](https://forum.needle.tools/t/how-to-make-a-button-reload-the-scene-the-button-starts-out-as-disabled-until-game-ends/1881/1 "2024-10-05T23:01:01Z")

</div>

I have a UI that is displayed when player dies, I need a button in the canvas to reload the scene. The button starts out as disabled until the player dies. Can you generate the code for this?

---

<div class="post-metadata">

**Author:** ![needle-assistant](https://forum.needle.tools/user_avatar/forum.needle.tools/needle-assistant/32/3415_2.png) [@needle-assistant](https://forum.needle.tools/u/needle-assistant)\
**Post date:** [October 5, 2024, 11:01pm UTC](https://forum.needle.tools/t/how-to-make-a-button-reload-the-scene-the-button-starts-out-as-disabled-until-game-ends/1881/2 "2024-10-05T23:01:03Z")

</div>

To create a button that reloads the scene in Needle Engine, starting disabled until the player dies, you can follow these steps in your code:

1. **Create a Button** : Define your button in the Unity editor or through code.
2. **Enable the Button** : Listen for the player’s death event to enable the button.
3. **Reload the Scene** : Define the functionality to reload the scene when the button is clicked.

Here’s a code example demonstrating these ideas:

```typescript
import { Behaviour, Button, SceneManager } from "@needle-tools/engine";

class ReloadButtonBehaviour extends Behaviour {
    private button: Button;

    async awake() {
        // Assuming the button is assigned via the Inspector
        this.button = this.getComponent(Button);
        this.button.setEnabled(false); // Disable initially
    }

    onPlayerDeath() {
        this.button.setEnabled(true); // Enable button when player dies
    }

    async onClick() {
        await SceneManager.reloadScene(); // Reload the current scene
    }
}

```

### Additional Considerations

- **Event Binding** : Ensure to call `onPlayerDeath` appropriately whenever the player dies in your game logic.
- **Button Click Event** : Set up the button’s click event to call `onClick()`.

### Final Integration

Attach the `ReloadButtonBehaviour` script to the button GameObject in your scene, link the button, and ensure to manage the player’s state and events correctly.

Feel free to ask for further clarification or help regarding your specific setup!

_3 replies left_  
_thread\_pRGrpFn4qLt034YYSzLrUPZt_  
_Reply to this message to continue the conversation_

---

<div class="post-metadata">

**Author:** ![Nick\_Romanek](https://forum.needle.tools/user_avatar/forum.needle.tools/nick_romanek/32/3583_2.png) [@Nick\_Romanek](https://forum.needle.tools/u/Nick_Romanek)\
**Post date:** [October 5, 2024, 11:02pm UTC](https://forum.needle.tools/t/how-to-make-a-button-reload-the-scene-the-button-starts-out-as-disabled-until-game-ends/1881/3 "2024-10-05T23:02:44Z")

</div>

@needle-assistant

Thanks, I’m getting this error:

Module ‘“@needle-tools/engine”’ has no exported member ‘SceneManager’

---

<div class="post-metadata">

**Author:** ![needle-assistant](https://forum.needle.tools/user_avatar/forum.needle.tools/needle-assistant/32/3415_2.png) [@needle-assistant](https://forum.needle.tools/u/needle-assistant)\
**Post date:** [October 5, 2024, 11:02pm UTC](https://forum.needle.tools/t/how-to-make-a-button-reload-the-scene-the-button-starts-out-as-disabled-until-game-ends/1881/4 "2024-10-05T23:02:45Z")

</div>

To reload the scene in Needle Engine without the `SceneManager`, you can utilize the `SceneSwitcher` class instead. Here’s how you can adapt your previous code:

```typescript
import { Behaviour, Button } from "@needle-tools/engine";
import { SceneSwitcher } from "@needle-tools/engine"; // Import SceneSwitcher

class ReloadButtonBehaviour extends Behaviour {
    private button: Button;
    private sceneSwitcher: SceneSwitcher;

    async awake() {
        this.button = this.getComponent(Button);
        this.button.setEnabled(false); // Disable initially

        // Get the SceneSwitcher from the current context
        this.sceneSwitcher = this.findObjectOfType(SceneSwitcher);
    }

    onPlayerDeath() {
        this.button.setEnabled(true); // Enable button when player dies
    }

    async onClick() {
        if(this.sceneSwitcher) {
            await this.sceneSwitcher.switchScene(this.sceneSwitcher.scenes[0]); // Reload the first scene
        }
    }
}

```

### Key Points:

- The `SceneSwitcher` is obtained using `findObjectOfType(SceneSwitcher)`.
- When the button is clicked, it reloads the first scene listed in the `scenes` property of the `SceneSwitcher`. Adjust the index or reference according to your scene setup.

This code will allow the button to reload the scene upon being clicked following the player’s death. Let me know if you need further assistance!

_2 replies left_  
_thread\_pRGrpFn4qLt034YYSzLrUPZt_  
_Reply to this message to continue the conversation_

---

<div class="post-metadata">

**Author:** ![needle-assistant](https://forum.needle.tools/user_avatar/forum.needle.tools/needle-assistant/32/3415_2.png) [@needle-assistant](https://forum.needle.tools/u/needle-assistant)\
**Post date:** [October 5, 2024, 11:04pm UTC](https://forum.needle.tools/t/how-to-make-a-button-reload-the-scene-the-button-starts-out-as-disabled-until-game-ends/1881/6 "2024-10-05T23:04:04Z")

</div>

Certainly, 0nederful! Here’s a simplified version of the code snippet that directly reloads the scene when the button is clicked:

```typescript
import { Behaviour, Button, SceneSwitcher } from "@needle-tools/engine";

class ReloadButtonBehaviour extends Behaviour {
    private button: Button;
    private sceneSwitcher: SceneSwitcher;

    async awake() {
        this.button = this.getComponent(Button);
        this.button.setEnabled(true); // Enable the button directly for simplicity

        // Get the SceneSwitcher instance
        this.sceneSwitcher = this.findObjectOfType(SceneSwitcher);
    }

    async onClick() {
        if (this.sceneSwitcher) {
            await this.sceneSwitcher.switchScene(this.sceneSwitcher.currentScene); // Reload the current scene
        }
    }
}

```

### Explanation:

- This version assumes the button is enabled. To disable it initially, incorporate your existing logic when the player dies.
- The scene is reloaded using the `switchScene` method, referencing the currently active scene.

You can attach this script to your button GameObject, and it should handle reloading the scene when clicked. Let me know if you need anything else!

_1 replies left_  
_thread\_pRGrpFn4qLt034YYSzLrUPZt_  
_Reply to this message to continue the conversation_

---

<div class="post-metadata">

**Author:** ![system](https://forum.needle.tools/user_avatar/forum.needle.tools/system/32/20_2.png) [@system](https://forum.needle.tools/u/system)\
**Post date:** [October 18, 2024, 11:04pm UTC](https://forum.needle.tools/t/how-to-make-a-button-reload-the-scene-the-button-starts-out-as-disabled-until-game-ends/1881/7 "2024-10-18T23:04:32Z")

</div>

Closed on October 18, 2024, 11:04pm UTC

This topic was automatically closed 13 days after the last reply. New replies are no longer allowed.
