1

I have followed and completed a Unity tutorial however once the tutorial is said and done, there was no function mentioned to restart the game.

Other than closing the application and re-opening, how would I go about adding something like this in?

Programmer
  • 121,791
  • 22
  • 236
  • 328
CDoc
  • 19
  • 1
  • 1
  • 4
  • 2
    Possible duplicate of [Unity3d restart current scene](https://stackoverflow.com/questions/41644156/unity3d-restart-current-scene) – Hellium May 31 '17 at 15:13

2 Answers2

9

There are do ways to restart game in Unity:

1.Reset every useful variables such as position, rotation, score to their default position. When you use this method, instead of #2, you will reduce how much time it take for your game to load.

Create a UI Button then drag it to the resetButton slot in the Editor.

//Drag button from the Editor to this
public Button resetButton;

Vector3 defaultBallPos;
Quaternion defaultBallRot;
Vector3 defaultBallScale;
int score = 0;


void Start()
{
    //Get the starting/default values
    defaultBallPos = transform.position;
    defaultBallRot = transform.rotation;
    defaultBallScale = transform.localScale;
}

void OnEnable()
{
    //Register Button Event
    resetButton.onClick.AddListener(() => buttonCallBack());
}

private void buttonCallBack()
{
    UnityEngine.Debug.Log("Clicked: " + resetButton.name);
    resetGameData();
}

void resetGameData()
{
    //Reset the position of the ball and set everything to the starting postion
    transform.position = defaultBallPos;
    transform.rotation = defaultBallRot;
    transform.localScale = defaultBallScale;

    //Reset other values below
}

void OnDisable()
{
    //Un-Register Button Event
    resetButton.onClick.RemoveAllListeners();
}

2.Call SceneManager.LoadScene("sceneName"); to load the scene again. You can call this function when Button.onClick.AddListener is called..

Create a UI Button then drag it to the resetButton slot in the Editor.

//Drag button from the Editor to this
public Button resetButton;

void OnEnable()
{
    //Register Button Event
    resetButton.onClick.AddListener(() => buttonCallBack());
}

private void buttonCallBack()
{
    UnityEngine.Debug.Log("Clicked: " + resetButton.name);

    //Get current scene name
    string scene = SceneManager.GetActiveScene().name;
    //Load it
    SceneManager.LoadScene(scene, LoadSceneMode.Single);
}

void OnDisable()
{
    //Un-Register Button Event
    resetButton.onClick.RemoveAllListeners();
}

The decision to which method to use depends on how much Objects in your scene and how much time it takes your scene to load. If the scene has a huge world with baked light maps and HQ textures then go with #1.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Personally I prefer using the SceneManager – Spencer Pollock May 31 '17 at 15:22
  • 5
    @SpencerPollock Actually, it's not a matter of preferences in this case. If you have huge scene with loaded baked maps and HQ textures, you should just reset the variables to save time loading time for the player. If that's a tiny scene then go with #2. – Programmer May 31 '17 at 15:25
  • 1
    Thanks for the reply. I should probably add I am a 100% novice and that tutorial was my first attempt ever. As such I am having a little difficulty understanding how to implement the steps you have outlined. Could you elaborate? I did try and look through the documents for further explanation. – CDoc May 31 '17 at 15:26
  • Which one? The first or the second? – Programmer May 31 '17 at 15:31
  • Well both to be honest. I tried to take the code you detailed in #2 and insert within my own script, however this did nothing - I am probably missing something. It's now frustrating me to the point I am ready to just leave it without a restart function :P – CDoc May 31 '17 at 15:37
  • 1
    @Programmer yes totally true when I think about it. This would have to be a pretty huge scene to see performance issues but I understand what you're saying – Spencer Pollock May 31 '17 at 15:37
  • 1
    That's right. Wonder what happens if EA starts doing this in their sport games. That would save loading times that's usually 1 to 2 minutes. @CDoc I have modified the question to add example for each one. – Programmer May 31 '17 at 16:01
  • Thanks for the elaboration. I have made the UI button and a new script to input the code. However I am still met with errors. This is starting to seem like more trouble than it may be worth! – CDoc May 31 '17 at 17:06
1

For example you can reload main scene for restart game.

SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
Mixon McLaren
  • 257
  • 2
  • 6