Having issues creating a reset button (Unity 5.3.1)
Basically I am trying to create a reset button so when you play the game, you can click the button OR press L to restart the level. I understand that you need a Game Controller object and then you attach a script to the controller and then you attach that to the button UI in the canvas. But what would my code look like in order for the button to be clickable/Key input-able and resets the level properly?
Answer by TBruce · May 06, 2016 at 04:14 AM
Your code would look something like below
 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class GameManager : MonoBehaviour
 {
     // refresh all the GUI elements
     public void RestartGame()
     {
         UnityEngine.SceneManagement.SceneManager.LoadScene(UnityEngine.SceneManagement.SceneManager.GetActiveScene().name);
     }
 }
Now this could either be called from a UI button or from a key press like this
 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class GameManager : MonoBehaviour
 {
     // Update is called once per frame
     public void Update()
     {
         // if the key L is pressed then te current level will restart
         if (Input.GetKeyUp(KeyCode.L))
         {
             RestartGame();
         }
     }
 
     // refresh all the GUI elements
     public void RestartGame()
     {
         UnityEngine.SceneManagement.SceneManager.LoadScene(UnityEngine.SceneManagement.SceneManager.GetActiveScene().name);
     }
 }
Your answer
 
 
             Follow this Question
Related Questions
AddListener fuction throwing a NullReferenceException error 1 Answer
World space canvas buttons are not working? 0 Answers
Button custom collider 0 Answers
Scene Buttons not working after scene switch 0 Answers
How to find an UI button by name? 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                