前提・実現したいこと
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最新版です。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/07/22 12:25