Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Alverik · Sep 27, 2016 at 07:29 AM · c#serializationscriptableobjectcustom editornull reference exception

CustomEditor for an ScriptableObject asset only works after recompile.

Hi, I'm really new to ScriptableObjects and Editor scripting and I'm having some trouble creating some "Setting" assets for some functionality in my project... Basically, it's got to do with some Lists of AudioClips. I want the designer to be able to drop the clips in and be able to reuse the asset anytime. But when you first create the asset and use the custom editor to try and change the amount of clips in the array, the compiler throws a null ref exception... But if Unity recompiles for any reason, then the Asset starts to work right! Only new Assets fail... until Unity recompiles for whatever reason...

Here is the SO:

 [System.Serializable]
     [CreateAssetMenu(menuName = "Sfx/ManagerData")]
     public class Sfx_SettingsAsset : ScriptableObject
     {
  
         //tag data
         public List<string> ManagerTags = new List<string>() { "Wood", "Concrete", "Grass", "Metal", "Water", "Mud", "Snow", "Fire" };
  
      
         //Allows for more than one sound to avoid machinegun effect
         [HideInInspector] public List<List<AudioClip>> MasterClips;
  
         public List<AudioClip> SfxDefault;
         public List<AudioClip> Sfx01;
         public List<AudioClip> Sfx02;
         public List<AudioClip> Sfx03;
         public List<AudioClip> Sfx04;
         public List<AudioClip> Sfx05;
         public List<AudioClip> Sfx06;
         public List<AudioClip> Sfx07;
         public List<AudioClip> Sfx08;
  
  
         public bool[] UseDefaultTag = { true, true, true, true, true, true, true, true };
         public int[] SfxAmount = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  
  
         /// <summary>
         /// The min volume of the sfx produced on contact.
         /// </summary>
         public float SfxMinVol = .5f;
         /// <summary>
         /// The max volume of the sfx produced on contact.
         /// </summary>
         public float SfxMaxVol = 1f;
  
         /// <summary>
         /// The min pitch of the sfx produced on contact.
         /// </summary>
         public float SfxMinPitch = .4f;
         /// <summary>
         /// The max pitch of the sfx produced on contact.
         /// </summary>
         public float SfxMaxPitch = 1f;
  
     }

Here is the part of the editor script which returns the error:

 //inside public override void OnInspectorGUI()
 //get access to the info in you script
         LimbSfxManager s = (LimbSfxManager)target;
  
 //......//
  
 s.MasterClips = new List<List<AudioClip>>() { s.SfxDefault, s.Sfx01, s.Sfx02,
             s.Sfx03, s.Sfx04, s.Sfx05, s.Sfx06, s.Sfx07, s.Sfx08 };
      
         foreach (var clips in s.MasterClips)
         {
             //Debug.Log(s.SfxAmount[i]);
             s.SfxAmount[i] = EditorGUILayout.DelayedIntField("Audio clips in " + (i == 0 ? "Default" : s.ManagerTags[i-1]) + " Keyword:", s.SfxAmount[i]);
  
             int i2 = 0;
             while (i2 < s.SfxAmount[i])
             {
  
                 if (clips.Count != s.SfxAmount[i]) //Null ref right HERE!
                 {
                     //make temp list
                     var tempList = s.MasterClips[i];
                     //get old size
                     int tempsize = s.MasterClips[i].Count;
                     //if the old size is less
                     if (tempsize < s.SfxAmount[i])
                     {
                         while (tempsize < s.SfxAmount[i])
                         {
                             //add a new clip until it matches the new size
                             AudioClip tempClip = new AudioClip();
                             tempList.Add(tempClip);
  
                             tempsize++;
                         }
                     }
                     //if the old size is more
                     if (tempsize > s.SfxAmount[i])
                     {
                         while (tempsize > s.SfxAmount[i])
                         {
                             //remove last clip until it matches the new size
                             tempList.RemoveAt(s.SfxAmount[i]);
                             tempsize--;
                         }
                     }
  
                     //assign the modified list as the EditorClips
                     s.MasterClips[i] = tempList;
                 }
                 if (s.SfxAmount != null)
                 {
                     s.MasterClips[i][i2] = (AudioClip)EditorGUILayout.ObjectField((i == 0 ? "Default" : s.ManagerTags[i-1]) + " Sound clip " + (i2 + 1) + ":", s.MasterClips[i][i2], typeof(AudioClip), false);
                 }
                 else
                 {
                     EditorGUILayout.HelpBox("There are no clip lists defined.", MessageType.Warning);
                 }
              
                 i2++;
             }
             EditorGUILayout.Space();
             EditorGUILayout.Space();
             i++;
         }

The rest of the code (with the tags) doesn't give any problems (so I left it out to save space). And I'm using a practically identical piece of code for the "Manager" gameobject sitting in the hierarchy, and that one doesn't seem to give me any errors... I just slot in the (fixed-after-recompile-for-whatever-reason) assets and it reads them and modifies them without errors.

I just don't get it, why do the asset files work fine after a recompile but not upon creation? I tried setting the target as dirty in OnEnable in the editor script and also tried marking stuff as [System.Serializeable]. But no difference... I only started programming a few months ago, so as you probably already noticed I still don't get serialization and other stuff very much yet...

Anyway, any advice will be greatly appreciated.

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 PineTreeDev · May 19 at 09:50 AM

Hello!

Old question, but I figured it could be answered


In your Scriptable Object manager, you have these lists of clips that are basically not initialized. When you are going through your clips in the editor class, and ask for the count of those lists, they are empty the first time after creating an asset!

So when you do your assembly reload, unity forces initialization on assets belonging to that assembly reload, hence the lists no longer throw a null reference, because the editor initialized them for you!

Fixing it is just a matter of, either in your editor script or in the Scriptable Object Reset function, going through all of those clips lists and initializing them.

 public List<AudioClip> SfxDefault;
 public List<AudioClip> Sfx01;
 public List<AudioClip> Sfx02;
 public List<AudioClip> Sfx03;
 public List<AudioClip> Sfx04;
 public List<AudioClip> Sfx05;
 public List<AudioClip> Sfx06;
 public List<AudioClip> Sfx07;
 public List<AudioClip> Sfx08;

Comment
Add comment · 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

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

224 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 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 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 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

Serializing a ScriptableObject without creating an asset for it? 2 Answers

"Unbroken Reference" problem when using a custom Editor to Save/Load a ScriptableObject Asset 0 Answers

ScriptableObjects lists (from the same script) open simultaneously in custom window 0 Answers

Null Reference Exception on a static array (Deserialize) - what am I doing wrong 1 Answer

Field not Serializing in a custom class 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