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
1
Question by bundy · Aug 12, 2016 at 01:35 AM · serializationscriptableobjectscriptable object

Serializing ScriptableObject into scene without writing to asset

Hey everyone,

I've been trying to get a ScriptableObject that's tied to a MonoBehaviour to serialize into the scene without writing it out as an asset. It seems like it's never serialized into the scene no matter what I do. I just wanted to see if it's just not how it's supposed to be used or if I'm missing something. Here's a basic rundown of how it's setup:

MonoBehaviour class that holds the ScriptableObject

 public class TestObj : MonoBehaviour {
     public TestSObj testSObj;
 }  

  
 

ScriptableObject class

 [Serializable]
 public class TestSObj : ScriptableObject {
     public string name;
 }  
  
 

CustomEditor for Monobehaviour

 [CustomEditor(typeof(TestObj))]
 public class TestObjEditor : Editor {
     TestObj testObj;

     public override void OnInspectorGUI() {
         testObj = (TestObj)target;

         DrawDefaultInspector();

         if (GUILayout.Button("Create")) { Create(); }
     }

     void Create() {
         testObj.testSObj = ScriptableObject.CreateInstance<TestSObj>();
         EditorUtility.SetDirty(testObj);
     }
 }  
 

I apologize in advance for the terrible class names. Any insight into this issue would be greatly appreciated, thanks!

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

Answer by DiegoSLTS · Aug 12, 2016 at 03:24 AM

ScriptableObjects are assets, they're not serialized in the scene. It's like trying to serialize a texture's pixels in the scene instead of having an asset for that texture in the project.

You create a ScriptableObject asset of type TestObj, setup it's values in the asset, then add a reference to that asset in any script in your scene. What gets serialized is the reference, like any reference to other assets.

You can have different TestObj assets with different values.

EDIT: Also, I guess you found the tutorial that teaches how to create ScriptableObjects by writing a class that inherits from ScriptableObject an another class that creates the asset. There's an attribute that can be used when defining ScriptableObjects that handles all this for you: CreateAssetMenu

Check the official introduction: http://unity3d.com/es/learn/tutorials/modules/beginner/live-training-archive/scriptable-objects

It's long, but it explains everything you need to know and below it's the code that uses the attribute.

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

53 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

Related Questions

Make the same serialized class as a ScriptableObject? 1 Answer

Why do I need to serialize a struct inside a ScriptableObject to save its data? 1 Answer

Best way to create runtime variables for Scriptable Objects?,What's the best way to create 'runtime values' for Scriptable Objects? 1 Answer

Serialize a list of scriptable objects to Json 0 Answers

Can Scriptable Object be use for this? 2 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