前提・実現したいこと
Unity 2019.2.9f1でNullReferenceException: Object reference not set to an instance of an object
Store.Update () (at Assets/Script/Store.cs:300)
というエラーが出ています
if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
コードが長いためエラーが出ている行の抜粋です
コードのほうにも//「ここが該当です」と書いてあります
発生している問題・エラーメッセージ
NullReferenceException: Object reference not set to an instance of an object Store.Update () (at Assets/Script/Store.cs:300)
C#
using System.Collections; using System.Collections.Generic; using UnityEngine; using Valve.VR; public class Store : MonoBehaviour { //private SteamVR_Action_Boolean actionToHaptic = SteamVR_Actions._default.InteractUI; //private SteamVR_Action_Vibration haptic = SteamVR_Actions._default.Haptic; public SteamVR_TrackedObject Controller; private SteamVR_Controller.Device device; protected Vector2 TouchPadValue => device.GetAxis(); protected float TriggerValue => device.GetAxis(Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger).x; //格納するためのオブジェクト public GameObject myCube; //スライドを格納する変数 public GameObject Pic; //スライド public GameObject SlideShow; public Ray ray; public RaycastHit hit; //本オブジェクト public GameObject book1; float raydis = 9; void Start() { if (Controller) { device = SteamVR_Controller.Input((int)Controller.index); } //本オブジェクト格納 myCube = GameObject.Find("book1"); //Findスライド Pic = GameObject.Find("SlideShow"); //スライドを消す Pic.SetActive(false); } void Pulse(ushort pulse) { device.TriggerHapticPulse(pulse); } void ControllerFunction(SteamVR_Controller.Device device) { if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger)) { TriggerTouchDown(); //Debug.Log("トリガーを浅く引いた"); } if (device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger)) { TriggerPressDown(); //Debug.Log("トリガーを深く引いた"); } if (device.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad)) { TouchpadTouchDown(); //Debug.Log("タッチパッドをクリックした"); } if (device.GetPress(SteamVR_Controller.ButtonMask.Touchpad)) { TouchpadPress(); //Debug.Log("タッチパッドをクリックしている"); } if (device.GetPressUp(SteamVR_Controller.ButtonMask.Touchpad)) { TouchpadPressUp(); //Debug.Log("タッチパッドをクリックして離した"); } if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Touchpad)) { TouchpadTouchDown(); //Debug.Log("タッチパッドに触った"); } protected virtual void TouchPadTouch() { return; } protected virtual void TriggerPress() { return; } protected virtual void TriggerTouch() { return; } protected virtual void GripPressDown() { return; } protected virtual void ApplicationMenuPressDown() { return; } protected virtual void TouchpadTouchUp() { return; } protected virtual void TouchpadPressUp() { return; } protected virtual void TouchpadPress() { return; } protected virtual void TouchpadTouchDown() { return; } protected virtual void TriggeTouchUp() { return; } protected virtual void TriggerPressDown() { return; } protected virtual void TriggerTouchDown() { return; } void Update() { if (device != null) { ControllerFunction(device); }else if(Controller){ device = SteamVR_Controller.Input((int)Controller.index); } if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))//ここが該当箇所です { ray = new Ray(transform.position, transform.TransformDirection(Vector3.forward)); Debug.DrawRay(ray.origin, ray.direction, Color.red, 3.0f); if (Physics.Raycast(ray, out hit, raydis)) { Debug.Log("当たり"); Debug.Log(hit.collider.gameObject.name); //セット1 if (myCube.activeInHierarchy) { myCube.SetActive(false); Pic.SetActive(true); } } else { if (!UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()) { hit = new RaycastHit(); if (Physics.Raycast(ray, out hit, raydis)) { Debug.Log("当たってない"); } else { myCube.SetActive(true); Pic.SetActive(false); } } } } } }
試したこと
マニュアルを試しましたがわかりません
補足情報(FW/ツールのバージョンなど)
Unity 2019.2.9f1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。