Search Unity

Feedback Restart button help

Discussion in '2D' started by JackAshwell, Oct 23, 2019.

  1. JackAshwell

    JackAshwell

    Joined:
    Oct 21, 2019
    Posts:
    24
    So i am trying to make a restart button, but there seems to a problem. My script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class Restart : MonoBehaviour {
    7.    
    8.     public void RestartGame() {
    9.         public scene = SceneManager.LoadScene("2D Space Shooter");
    10.     }
    11. }
    And the unity interface with the console errors and inspector:

    https://imgur.com/a/Oek2Qfy


    Thanks!
     
    eaglethrust likes this.
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    public scene = SceneManager.LoadScene("2D Space Shooter");


    This bit of code doesn't fully make sense as defined.

    When defining a local variable, you need to give the variable a Type, and you can't declare something "public" inside a function.

    Also, the "LoadScene" method doesn't return a value, so you can't assign it to a variable anyway. So get rid of "public scene =" and it will probalby work.
     
    eaglethrust likes this.
  3. JackAshwell

    JackAshwell

    Joined:
    Oct 21, 2019
    Posts:
    24
    Yeah don't worry i fixed that one:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class Restart : MonoBehaviour {
    7.  
    8.     void OnGUI() {
    9.         if(GUI.Button(new Rect(269, 10, 75, 30), "Restart")) {
    10.             SceneManager.UnloadSceneAsync("2D Space Shooter");
    11.             SceneManager.LoadScene("2D Space Shooter");
    12.         }
    13.     }
    14. }
    I removed the public scene, but then it still didn't work so i created a button. But then when i clicked that it just repeated what was on screen anyway. So i had to unload the scene then load it again.
     
    eaglethrust likes this.