Htc Vive Controller script issue

Hello everybody,

I had a script that makes the UI menu show each time Esc key is pressed.
Now I’d like to implement that with Htc Vive controllers, using the menu button.

Here’s the script in C#:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif

public class PauseManager : MonoBehaviour
{
    Canvas canvas;

    private SteamVR_Controller.Device controller
    {
        get { return SteamVR_Controller.Input((int)trackedObj.index); }
    }

   public SteamVR_TrackedObject trackedObj;

    void Awake()
    {
        trackedObj = GetComponent<SteamVR_TrackedObject>();
    }

    void Start()
    {
        canvas = GetComponent<Canvas>();
        canvas.enabled = false;
    }

    void Update()
    {
        if (controller == null)
        {
            Debug.Log("Controller not initialized");

            return;
        }

        if (controller.GetPressDown(SteamVR_Controller.ButtonMask.ApplicationMenu))

        {
            Pause();
            Debug.Log("Fire");
        }       
    }

    public void Pause()
    {
        canvas.enabled = !canvas.enabled;
        Time.timeScale = Time.timeScale == 0 ? 1 : 0;
    }

    public void Quit()
    {
#if UNITY_EDITOR
        EditorApplication.isPlaying = false;
#else
        Application.Quit();
#endif
    }
}

It look fairly simple, but it’s not working and the console is displaying:
NullReferenceException: Object reference not set to an instance of an object
PauseManager.get_controller () (at Assets/Scripts/MonoBehaviour/PauseManager.cs:15)
PauseManager.Update () (at Assets/Scripts/MonoBehaviour/PauseManager.cs:35)

Hi @edwardpaulnelson @Jamy4000

In the update function:

void Update()
    {
   if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.JoystickButton0) || Input.GetKeyDown(KeyCode.JoystickButton2))
        {
            Pause();
        }
    
    }

Hi, @Eco-Editor

Know this was a long time ago but really struggling with similar issue.

If you have a fix can you please post.