Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question how do you add an button shortcut

Discussion in 'Editor & General Support' started by Rowanlikesdonuts, Aug 18, 2020.

  1. Rowanlikesdonuts

    Rowanlikesdonuts

    Joined:
    Jul 31, 2020
    Posts:
    15
    I added an button on the UI and the button works fine but how do you set an shortcut to it like "t"?
     
  2. Rowanlikesdonuts

    Rowanlikesdonuts

    Joined:
    Jul 31, 2020
    Posts:
    15
    Here is the code I used:
    Code (CSharp):
    1. using UnityEngine.SceneManagement; using UnityEngine;
    2.  
    3. public class RESTART : MonoBehaviour {
    4.  
    5. public void restart()
    6. {
    7.     SceneManager.LoadScene (SceneManager.GetActiveScene ().name);
    8.     Scene loadedLevel = SceneManager.GetActiveScene ();
    9.     SceneManager.LoadScene (loadedLevel.buildIndex);
    10.     SceneManager.LoadScene("GAME");
    11.     SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    12.  
    13. }
    14. I added it as an on click event and i use it as an restart button. I only have one scene.
    15. }
     
  3. RakNet

    RakNet

    Joined:
    Oct 9, 2013
    Posts:
    313
    if (Input.GetKeyDown(KeyCode.T)
    referenceToTheButton.onClick.Invoke();
     
  4. Rowanlikesdonuts

    Rowanlikesdonuts

    Joined:
    Jul 31, 2020
    Posts:
    15
    Thanks! I will start up my laptop soon.
     
  5. Rowanlikesdonuts

    Rowanlikesdonuts

    Joined:
    Jul 31, 2020
    Posts:
    15
    It does not work. I copied it into my code and it shows an error saying that referenceToTheButton is not availible.
     
    Last edited: Aug 19, 2020
  6. RakNet

    RakNet

    Joined:
    Oct 9, 2013
    Posts:
    313
    You need in your code
    public UnityEngine.UI.Button referenceToTheButton;
    Then assign it in the inspector
     
  7. Rowanlikesdonuts

    Rowanlikesdonuts

    Joined:
    Jul 31, 2020
    Posts:
    15

    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.
     
  8. RakNet

    RakNet

    Joined:
    Oct 9, 2013
    Posts:
    313