Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Taorcb · Nov 20, 2016 at 11:57 PM · soundnullreferenceexceptiongeneral programming

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Eric5h5 · Nov 21, 2016 at 12:06 AM

Resources.Load can only load from Resources, not an arbitrary asset path.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Taorcb · Nov 21, 2016 at 01:20 AM 0
Share

It's being saved to the resources folder. I'm not sure if Resources.Load can load ScriptableObjects, though.

avatar image Eric5h5 Taorcb · Nov 21, 2016 at 01:35 AM 0
Share

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.

avatar image Taorcb Eric5h5 · Nov 21, 2016 at 04:54 AM 0
Share

I see what you mean. Even fixing that line (using activeFile = (LipSync)Resources.Load("Resources/SaveData/LipSyncData/" + clip.name + " (LipSync)");) still throws the error.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

57 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges