- Home /
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)
Hello Eco-Editor ! I just wanted to know if you have succeeded to find a solution to your problem, and if you were willing to share it here please ? :) Thank you !
Answer by Eco-Editor · Mar 22, 2018 at 06:36 PM
Hi @edwardpaulnelson @Jamy4000
In the update function:
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.JoystickButton0) || Input.GetKeyDown(KeyCode.JoystickButton2))
{
Pause();
}
}
Hey @Eco-Editor and @edwardpaulnelson,
I don't really remember how I succeeded to fix the issue to be fair, but I don't think the code you just posted will fix the exception issue ...
If that can help, here's the link to the script I'm using in every VR project, that get the Vive Inputs.
Just don't forget to assign the controllers scripts in the editor !
Answer by edwardpaulnelson · Mar 22, 2018 at 04:34 PM
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.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
how to save the values of hmd and controller position rotation in a list 0 Answers
Player Movement 0 Answers