質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

654閲覧

NullReferenceExceptionというエラーが出る

gori678

総合スコア7

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/01/09 14:26

前提・実現したいこと

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

nullの変数を参照しているというエラーです
そのエラーの出る行で実行を止め、そこで使われてる変数のナカミを確認しましょう

投稿2020/01/09 14:46

y_waiwai

総合スコア87774

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問