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

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

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

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

オブジェクト指向

オブジェクト指向プログラミング(Object-oriented programming;OOP)は「オブジェクト」を使用するプログラミングの概念です。オブジェクト指向プログラムは、カプセル化(情報隠蔽)とポリモーフィズム(多態性)で構成されています。

Unity

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

Q&A

解決済

2回答

982閲覧

インスペクタで取得したクラスのフィールドにアクセスできない。

mentanpinsan

総合スコア23

C#

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

オブジェクト指向

オブジェクト指向プログラミング(Object-oriented programming;OOP)は「オブジェクト」を使用するプログラミングの概念です。オブジェクト指向プログラムは、カプセル化(情報隠蔽)とポリモーフィズム(多態性)で構成されています。

Unity

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

0グッド

0クリップ

投稿2020/07/21 14:28

前提・実現したいこと

GameObjectはAudioとLineです。
Lineにアタッチしたスクリプト(KochLine)からAudioにアタッチしたスクリプト(Audio2)のフィールド(_audioBandBuffer)にアクセスしたい。

発生している問題・エラーメッセージ

NullReferenceException: Object reference not set to an instance of an object KochLine.Update () (at Assets/KochLine.cs:38) Assets\KochLine.cs(14,21): warning CS0649: Field 'KochLine._lerpAudio' is never assigned to, and will always have its default value null

該当のソースコード

KochLIne

1 using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5[RequireComponent(typeof(LineRenderer))] 6public class KochLine : KochGenerator 7{ 8 // Start is called before the first frame update 9 LineRenderer _lineRenderer; 10 // [Range(0, 1)] 11 // public float _lerpAmount; 12 Vector3[] _lerpPosition; 13 public float _generateMultiplier; 14 private float[] _lerpAudio; 15 16 [Header("Audio")] 17 public Audio2 _audio; 18 public int[] _audioBand; 19 20 void Start() 21 { 22 _lineRenderer = GetComponent<LineRenderer>(); 23 _lineRenderer.enabled = true; 24 _lineRenderer.positionCount = _position.Length; 25 _lineRenderer.SetPositions(_position); 26 _lerpPosition = new Vector3[_position.Length]; 27 28 } 29 30 // Update is called once per frame 31 void Update() 32 { 33 if (_generationCount != 0)//eveyframe 34 { 35 int count = 0; 36 for (int i = 0; i < _initiatorPointAmount; i++) 37 { 38 _lerpAudio[i] = _audio._audioBandBuffer[_audioBand[i]]; 39 for (int j = 0; j < (_position.Length - 1) / _initiatorPointAmount; j++) 40 { 41 42 _lerpPosition[count] = Vector3.Lerp(_position[count], _targetPosition[count], _lerpAudio[i]); 43 count++; 44 } 45 // _lerpPosition[i] = Vector3.Lerp(_position[i], _targetPosition[i], _lerpAmount); 46 } 47 _lerpPosition[count] = Vector3.Lerp(_position[count], _targetPosition[count], _lerpAudio[_initiatorPointAmount - 1]); 48 49 if (_useBezierCurves) 50 { 51 _bezierPosition = BezierCurve(_lerpPosition, _bezierVertexCount); 52 _lineRenderer.positionCount = _bezierPosition.Length; 53 _lineRenderer.SetPositions(_bezierPosition); 54 } 55 else 56 { 57 _lineRenderer.positionCount = _lerpPosition.Length; 58 _lineRenderer.SetPositions(_lerpPosition); 59 } 60 } 61 62 63 } 64} 65

Audio2

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4[RequireComponent(typeof(AudioSource))] 5public class Audio2 : MonoBehaviour 6{ 7 AudioSource _audioSource; 8 public float[] _samples = new float[512]; 9 public float[] _freqBand = new float[8]; 10 public float[] _bandBuffer = new float[8]; 11 float[] _bufferDecrease = new float[8]; 12 float[] _freqBandHighest = new float[8]; 13 public float[] _audioBand = new float[8];//0-1 14 public float[] _audioBandBuffer = new float[8]; 15 16 // Start is called before the first frame update 17 void Start() 18 { 19 _audioSource = GetComponent<AudioSource>(); 20 } 21 22 // Update is called once per frame 23 void Update() 24 { 25 GetSpectrumAudioSource(); 26 MakeFrequencyBands(); 27 BandBuffer(); 28 CreateAudioBands(); 29 } 30 31 void CreateAudioBands() 32 { 33 for (int i = 0; i < 8; i++) 34 { 35 if (_freqBand[i] > _freqBandHighest[i]) 36 { 37 _freqBandHighest[i] = _freqBand[i]; 38 } 39 _audioBand[i] = (_freqBand[i] / _freqBandHighest[i]); 40 _audioBandBuffer[i] = (_bandBuffer[i] / _freqBandHighest[i]); 41 } 42 } 43 44 void GetSpectrumAudioSource() 45 { 46 _audioSource.GetSpectrumData(_samples, 0, FFTWindow.Blackman); 47 } 48 49 void BandBuffer() 50 { 51 for (int g = 0; g < 8; g++) 52 { 53 if (_freqBand[g] > _bandBuffer[g]) 54 { 55 _bandBuffer[g] = _freqBand[g]; 56 _bufferDecrease[g] = 0.005f; 57 } 58 if (_freqBand[g] < _bandBuffer[g]) 59 { 60 _bandBuffer[g] -= _bufferDecrease[g]; 61 _bufferDecrease[g] *= 1.05f; 62 } 63 } 64 } 65 66 void MakeFrequencyBands() 67 { 68 int count = 0; 69 for (int i = 0; i < 8; i++) 70 { 71 float average = 0; 72 int sampleCount = (int)Mathf.Pow(2, i) * 2; 73 if (i == 7) 74 { 75 sampleCount += 2; 76 } 77 for (int j = 0; j < sampleCount; j++) 78 { 79 average += _samples[count] * (count + 1); 80 count++; 81 } 82 83 average /= count; 84 85 _freqBand[i] = average * 10; 86 87 } 88 } 89} 90

試したこと

エラーのKochLine.cs:38の該当箇所は
_lerpAudio[i] = _audio._audioBandBuffer[_audioBand[i]];
です。
LineオブジェクトのインスペクタにはAudioをドラッグアンドドロップしています。

補足情報(FW/ツールのバージョン)

unity最新版です。

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

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

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

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

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

guest

回答2

0

ベストアンサー

KochLineの_lerpAudioが初期化されていないのでは?
Audio2の_audioBandBufferの様に、必要な要素数分配列を確保する必要があります。

例:private float[] _lerpAudio = new float[8];

投稿2020/07/22 00:57

_keyk_

総合スコア7

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

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

mentanpinsan

2020/07/22 12:25

ありがとうございました。C#に慣れてなくて忘れていました。
guest

0

エラー発生個所はコードの後ではなくコードの前、エラーの直後に書いて欲しい。上から下に読み進めるように文章を書いて欲しい。

「該当箇所」で null の可能性があるオブジェクトはコードをざっとみると _audio のみです。これくらいは明示しておいて欲しいです。

まず調べるべき事は、エラー発生個所の直前で本当に _audio が null なのかチェックすることです。そして、本当に _audio が null ならば、その gameObject の名前をログに出力しておきます。合わせて Start 関数内で _audio のオブジェクト名もログに出力しておきましょう。

質問者が把握している限り _audio にはオブジェクトをアサインしているはず、とのことですが、それを踏まえても以下のようなケースが考えられます。もちろん他にもあると思います。

  • アサインしておいたオブジェクトが破棄された
  • 全然関係ないオブジェクトに操作ミスで KochLine をアタッチしてしまい、そこには _audio をアサインしていなかった

投稿2020/07/21 15:13

編集2020/07/21 18:09
bboydaisuke

総合スコア5275

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

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

mentanpinsan

2020/07/22 12:27

デバッグの仕方参考になりました。ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問