Home > Mobile >  How do I make a restart button or use r for restarting the scene
How do I make a restart button or use r for restarting the scene

Time:06-08

See when my player dies you can restart well that's what I wanted but I can't get it to work and I want it to where you have an option of either pressing r or the button to restart.

Can I please get a couple of tips for this probably some code too but that's up to you btw I am a beginner and need much help my game is just a basic learning game and I will love help from you fellow gamedevs

This is my old code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ResetManager : MonoBehaviour
{
    public static ResetManager Instance { get; private set; }

    public event System.Action ResetEvent = new delegate {};

    public void Awake()
    {
        Instance = this;
    }

    public static void ResetScene()
    {
        Instance.ResetEvent();
    }
}

I would like to know if I could make it without scene manager

CodePudding user response:

Welcome to stack overflow, here I will show a brief tutorial on how to build the key in the UI as well as execute the reset code. To create a UI button, go to Hierarchy and right-click, you can create a Button from the UI menu as shown below.

enter image description here

After creating the button, attach the script to a Manager object, click the button and reference the reset command at the bottom of the event, as below, keeping in mind that the unity event does not support static method's.

public void ResetScene() => SceneManager.LoadScene(SceneManager.GetActiveScene().name);

enter image description here

After performing the above steps, your button will restart the scene when clicked.


You can also restart the scene when you press the R key by adding the following code.

private void Update()
{
    if (Input.GetKeyDown(KeyCode.R)) ResetScene();
}

CodePudding user response:

i see where your coming from but i have adhd and somthing else where its hard for me to focus and when too much info is thrown at me i cant handle it but do you think that i can have a differant way

  • Related