So I’m trying to create a method of creating and saving files for lip sync data as ScriptableObjects. The issue is, after creating one on the click of an editor GUI button, I get a nullreference exception when trying to assign data to it.
Here’s the code. The exception is thrown at activeFile.volumeCurve.AddKey(source.time / clip.length, vol);
void OnGUI(){
source = (AudioSource)EditorGUILayout.ObjectField("Audio Source", source, typeof(AudioSource), true);
clip = (AudioClip)EditorGUILayout.ObjectField("Audio Clip", clip, typeof(AudioClip), false);
if(source && !source.isPlaying) {
if(GUILayout.Button("Generate File")){
activeFile = (LipSync)ScriptableObject.CreateInstance<LipSync>();
activeFile.name = clip.name + " (LipSync)";
string assetPath = filePath +"/" + activeFile.name + ".asset";
AssetDatabase.CreateAsset(activeFile, assetPath);
AssetDatabase.Refresh();
activeFile = (LipSync)Resources.Load(assetPath);
source.clip = clip;
source.Play();
}
}
}
void Update(){
if (source && source.isPlaying) {
AnalyzeSound();
UpdateFile();
}
}
void AnalyzeSound()
{
//A whole bunch of math, basically it spits out the volume and frequency of the audio source's output.
}
void UpdateFile(){
float vol = Mathf.Clamp01(DbValue / 140); //140 decibals is the loudest a human can hear without causing brain damage, so set it as the maximum because we want a 0-1 value.
//Calculate the frequency values on a 0-1 scale. 879db is the highest value on the F1 scale, so use that as the threshold. Then do simple division for each phonetic value we want.
float ee = Mathf.Clamp01(PitchValue > 879f? eeF2 / PitchValue : eeF1 / PitchValue);
float oo = Mathf.Clamp01(PitchValue > 879f? ooF1 / PitchValue : ooF2 / PitchValue);
float aa = Mathf.Clamp01(PitchValue > 879f? aaF1 / PitchValue : aaF2 / PitchValue);
activeFile.volumeCurve.AddKey(source.time / clip.length, vol);
activeFile.eeCurve.AddKey(source.time / clip.length, ee);
activeFile.ooCurve.AddKey(source.time / clip.length, oo);
activeFile.aaCurve.AddKey(source.time / clip.length, aa);
}
If it helps, the script plays the audio and then does the calculations as it’s being played (AnalyzeSound() does its calculations in realtime). I know that for some reason I’m losing the activeFile reference, but I have no clue why. I know this is kind of a dumb question, but I’m really lost on this one. Any ideas?