LoginSignup
riririki
@riririki (riki nona)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

エラー「NullReferenceException: Object reference not set to an object」

解決したいこと

Unityで、ARで撮影したものをRenderTextureに変換するコードでエラーが発生します。
「NullReferenceException: Object reference not set to an object」です。

ほとんどこちらの、撮影データを変換する部分のコピペになります
https://edom18.hateblo.jp/entry/2020/08/23/121008
Unityのバージョンは2021/1/11です。

発生している問題・エラー

下記コードの問題箇所を特定するため、「1」、「2」、「3」とログをとりました。
すると、コンソールに「3.5」まで表示されました。

そして、3.5の直後に、

「NullReferenceException: Object reference not set to an object」
というエラーが出ます。

該当するソースコード

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
using Unity.VisualScripting;

public class CaptureXRCamera : MonoBehaviour
{
   
    [SerializeField] private ARCameraManager _cameraManager = null;
    [SerializeField] private GameObject _target = null;
    [SerializeField] private Texture2D _sampleTexture = null;
    [SerializeField] private Material _transposeMaterial = null;
    [SerializeField] private Text _text = null;
    [SerializeField] private GameObject _button_ob = null;

    private Texture2D _texture = null;
    private RenderTexture _previewTexture = null;
    private Renderer _renderer = null;
    private Material _material = null;

    private bool _needsRotate = true;

    public void RefreshCameraFeedTexture()
    {
        Debug.Log("1");
        // TryGetLatestImageで最新のイメージを取得します。
        // ただし、失敗の可能性があるため、falseが返された場合は無視します。
        if (!_cameraManager.TryAcquireLatestCpuImage(out XRCpuImage cameraImage)) return;

        Debug.Log("2");

        // 中略

        // デバイスの回転に応じてカメラの情報を変換するための情報を定義します。
        XRCpuImage.Transformation imageTransformation = (Input.deviceOrientation == DeviceOrientation.LandscapeRight)
            ? XRCpuImage.Transformation.MirrorY
            : XRCpuImage.Transformation.MirrorX;
        Debug.Log("3");

        // カメライメージを取得するためのパラメータを設定します。
        XRCpuImage.ConversionParams conversionParams =
            new XRCpuImage.ConversionParams(cameraImage, TextureFormat.RGBA32, imageTransformation);

        Debug.Log("3.5");

        // 生成済みのTexture2D(_texture)のネイティブのデータ配列の参照を得ます。
        NativeArray<byte> rawTextureData = _texture.GetRawTextureData<byte>();

        Debug.Log("4");

        try
        {
            unsafe
            {
                Debug.Log("5");
                // 前段で得たNativeArrayのポインタを渡し、直接データを流し込みます。
                cameraImage.Convert(conversionParams, new IntPtr(rawTextureData.GetUnsafePtr()), rawTextureData.Length);
            }
        }
        finally
        {
            cameraImage.Dispose();
            Debug.Log("6");
        }

        // 取得したデータを適用します。
        _texture.Apply();
        Debug.Log("OK");

        // 後略
    }

}

自分で試したこと

ここに問題・エラーに対して試したことを記載してください。

0

2Answer

おそらく_texturenullなのが原因かと思われます。

以下の記事のように、事前にインスタンスを生成してあげることでうまくいかないでしょうか?

_texture =  new Texture2D(cameraImage.width, cameraImage.height, TextureFormat.RGBA32, false);
0

あなたは私の救世主です・・・なんとお礼を申し上げたら良いか・・・!!
いただいたコードを3.5の直後に設置したところ、最後までコードが走りました。
ほんんんんんとうにありがとうございます!!!!!!

0

Your answer might help someone💌