I added an button on the UI and the button works fine but how do you set an shortcut to it like “t”?
Here is the code I used:
using UnityEngine.SceneManagement; using UnityEngine;
public class RESTART : MonoBehaviour {
public void restart()
{
SceneManager.LoadScene (SceneManager.GetActiveScene ().name);
Scene loadedLevel = SceneManager.GetActiveScene ();
SceneManager.LoadScene (loadedLevel.buildIndex);
SceneManager.LoadScene("GAME");
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
I added it as an on click event and i use it as an restart button. I only have one scene.
}
if (Input.GetKeyDown(KeyCode.T)
referenceToTheButton.onClick.Invoke();
Thanks! I will start up my laptop soon.
It does not work. I copied it into my code and it shows an error saying that referenceToTheButton is not availible.
You need in your code
public UnityEngine.UI.Button referenceToTheButton;
Then assign it in the inspector
using UnityEngine.SceneManagement; using UnityEngine;
public class RESTART : MonoBehaviour {
public void restart()
{
public UnityEngine.UI.Button referenceToTheButton;
if (Input.GetKeyDown(KeyCode.T)
referenceToTheButton.onClick.Invoke();
SceneManager.LoadScene (SceneManager.GetActiveScene ().name);
Scene loadedLevel = SceneManager.GetActiveScene ();
SceneManager.LoadScene (loadedLevel.buildIndex);
SceneManager.LoadScene("GAME");
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
does not work. it gives 26 errors.