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?
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);
}
}