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 Tourist · Jan 24, 2013 at 04:46 PM · listinspectorscriptableobject

[CustomInspector] add scriptableobject in a list always null

I have a class A with a list of ScriptableObject's child class that we will call SoChild. I am writing a custom inspector for the class A.

The function OnInspectorGUI() :

 // update from object
 serializedObject.Update();
         
 base.OnInspectorGUI();
         
 //Generation
 m_bFoldout= EditorGUILayout.Foldout(m_bFoldout, "List");
 if(m_bFoldout)
 {
   SerializedProperty modules = serializedObject.FindProperty("myList");
             
   for(int iIndex = 0; iIndex < modules.arraySize; ++iIndex)
   {
     SerializedProperty element = modules.GetArrayElementAtIndex(iIndex);
     EditorGUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
     EditorGUILayout.PropertyField(element);
                 
     if(GUILayout.Button("-"))
     {
       // remove one element
     }
     EditorGUILayout.EndHorizontal();
   }
             
   if(GUILayout.Button("Add"))
   {
     int iIndex = modules.arraySize;
     modules.InsertArrayElementAtIndex(iIndex);
     SerializedProperty newElement = modules.GetArrayElementAtIndex(iIndex);
     SoChild newSoChild = ScriptableObject.CreateInstance<SoChild>();
     newElement.objectReferenceValue = newSoChild ;
     newElement.objectReferenceInstanceIDValue = newSoChild .GetInstanceID();
   }
 }
 
 // apply modifications to object
 serializedObject.ApplyModifiedProperties();

When i click on the button "Add", a new element is added to the list but the content of the property field is "None" (null). I don't understand why.

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

Answer by PAEvenson · Feb 04, 2013 at 06:21 PM

You use ScriptableObject.CreateInstance for when you want to create an instance of the scriptableObject at runtime. To create a new ScriptableObject in the editor you need to create the asset and add it to the project using AssetDatabase.CreateAsset.

This could help you: http://wiki.unity3d.com/index.php?title=CreateScriptableObjectAsset

Comment
Add comment · Show 6 · 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 Tourist · Feb 05, 2013 at 07:34 AM 0
Share

Thank you for your reply.

I'd like to create instances that are children of the serialiazedObject.target. Is there any way to do that, even if the children are not ScriptableObject? Children can be different types with a common interface.

avatar image PAEvenson · Feb 06, 2013 at 02:07 PM 0
Share

I am a little confused on what exactly you are trying to accomplish. I think of scriptableObject as a data file you reference at runtime, like storing the stats of an enemy. It lives in the project, not the scene, so if I access that scriptableObject in my level1 scene and level5 scene, that data will be the same. If I wanted to have a stronger version of that enemy for level 5 I would duplicate the scriptableObject, rename it to something like "StrongEnemy" and tweak the stats to my liking. Now it sounds like you are trying to create children of a reference in the scene. Is this what you are trying to accomplish?

avatar image Bunny83 · Feb 06, 2013 at 03:40 PM 0
Share

The short answer is: no!

Unity doesn't support inheritance for custom classes which are marked as serialized and not derived from $$anonymous$$onoBehaviour or ScriptableObject. The alternative is to either use classes that are derived from $$anonymous$$onoBehaviour or ScriptableObject. $$anonymous$$onoBehaviours of course need to be attached to a gameobject and are serialized on their own along with the scene or a prefab. ScriptableObjects aren't serialized on their own since they don't belong to anything. You have to save them manually as assets in the editor, there's no way around that.

avatar image Tourist · Feb 06, 2013 at 03:47 PM 0
Share

Is there anyway to manually serialize custom classes using inheritance? Is there some unity package to deal with it?

avatar image Mikeysee · Aug 24, 2014 at 01:15 AM 1
Share

You can now manually serialize: http://blogs.unity3d.com/2014/06/24/serialization-in-unity/

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

11 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

Related Questions

Difference between assigning a value in inspector and with a custom editor script 1 Answer

A node in a childnode? 1 Answer

Loop through a List to check whether a bool is true within a ScriptableObject 1 Answer

Unity inspector field overlap with eachother 1 Answer

Making enum visible in the Inspector via a drop down list 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