- Home /
Nullreference exception after creating asset
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?
Answer by Eric5h5 · Nov 21, 2016 at 12:06 AM
Resources.Load can only load from Resources, not an arbitrary asset path.
It's being saved to the resources folder. I'm not sure if Resources.Load can load ScriptableObjects, though.
You're still using an arbitrary asset path regardless of whether it contains a Resources folder. Resources.Load can't use a path like that. It can load any object, but needs a valid Resources string. See the docs for Resources.Load for examples of valid strings you can use.
I see what you mean. Even fixing that line (using activeFile = (LipSync)Resources.Load("Resources/SaveData/LipSyncData/" + clip.name + " (LipSync)");
) still throws the error.
Your answer

Follow this Question
Related Questions
Adding sound to animation: NullReferenceException 0 Answers
NullReferenceException only at first run 2 Answers
Global Variables & Object reference not set to an instance of an object 2 Answers
OnClick, parameter setup in editor becomes null?? 1 Answer
Problem accessing public method of script attached to gameobject. 0 Answers