Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 vinicius.ras · Nov 27, 2014 at 03:56 PM · serializationscriptableobjectresourcesassetdatabasecollections

Serializing a collection of ScriptableObjects to an ASSET file?

Hi all,

I have tried to make my code as simple/reduced as possible to try to understand what's going on. I have a class representing the data I want to serialize, like this:

 using UnityEngine;
 
 public class TestData : ScriptableObject
 {
     public int theValue;
 }


... and a class which represents a serializable collection of TestData objects, like this:

 using UnityEngine;
 
 public class TestCollection : ScriptableObject
 {
     public TestData [] myArray;
 }

Ok, so what I want to do is to create a collection of data objects and save this collection to an ASSET file, which will be loaded in runtime through Unity's Resources.Load() method. I have created a simple script which creates two menus in Unity Editor: one of them is used to create an asset file containing a predefined TestCollection of TestData objects, and the other one is used to load the saved TestCollection from that created file and print the values of all the TestData objects contained in it.

 using UnityEngine;
 using UnityEditor;
 
 public class TestMenu
 {
     [MenuItem("TEST/Create asset")]
     public static void testCreate()
     {
         TestCollection newAsset = ScriptableObject.CreateInstance<TestCollection>();
         
         newAsset.myArray = new TestData[3];
         for ( int t = 0; t < 3; t++ )
         {
             newAsset.myArray[t] = ScriptableObject.CreateInstance<TestData>();
             newAsset.myArray[t].theValue = (t+1)*100;   // initialize data objects with values 100, 200 and 300...
         }
         
         AssetDatabase.CreateAsset( newAsset, "Assets/Resources/MyTestAsset.asset" );
     }
     
     
     [MenuItem("TEST/Print values")]
     public static void testPrint()
     {
         TestCollection theAsset = Resources.Load<TestCollection>( "MyTestAsset" );
         foreach ( TestData x in theAsset.myArray )
             Debug.Log( x.theValue.ToString() );   // <-- EXCEPTION IS THROWN HERE
     }
 }


Now, the problem is that Unity seems to be serializing the TestCollection object correctly, while it is not serializing the TestData objects in it. If I click the "Create asset" menu and then click the "Print values" menu, the values of the objects stored in the asset file are printed correctly: 100, 200 and 300, respectivelly. But then, if I enter in play mode and click the "Print values" menu, a NullReferenceException is thrown when trying to execute the "Debug.Log( x.theValue.ToString() )" line ("x" is null), which means that the serialization of the TestData objects have failed!

So, is there any way to serialize a collection of ScriptableObject objects to an asset file? What am I missing here..?

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
Best Answer

Answer by Bunny83 · Nov 27, 2014 at 04:24 PM

ScriptableObjects need to be serialized on their own. So you either have to serialize them as seperate assets or (what will be more common) add them to your base asset with AssetDatabase.AddObjectToAsset.

Comment
Add comment · Show 1 · 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 vinicius.ras · Nov 27, 2014 at 04:49 PM 0
Share

Hi, Bunny83, thank you for your quick reply! I have tried what you suggested before trying to create a collection based on ScriptableObject. But when I tried to retrieve my data back by using "Resources.LoadAll()", only the first object from the asset was being returned.

By reading your answer and the documentation again, I found out that was missing something from the docs: after adding multiple assets to a file, you need to reimport that file by calling AssetDatabase.ImportAsset(), so that the asset gets updated by Unity with all the added objects! Shame on me for not noticing this before haha

Thank you very much

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

AssetDatabase.LoadAssetAtPath() not working after any sort of project change. 0 Answers

Scriptable Object references if not saved in AssetDatabase? 1 Answer

.asset file containing ScriptableObject is empty on openingUnity 1 Answer

Using System.Object in ScriptableObject 1 Answer

Custom assets give Missing (Mono Script) 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