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 zombience · Nov 11, 2013 at 10:39 PM · c#editorserializationscriptableobject

pass child class to ScriptableObject.CreateInstance<> ?

I'm building a custom editor to assemble a list of custom scripts and put them in a List on a script deriving from MonoBehaviour.

So far, my editor is working well. The point I'm stuck at is passing a variable from a list of types to ScriptableObject.CreateInstance

code snippets:

     public class MyEditorClass: Editor
     {
     
         private List<Type> behaviorTypes = new List<Type>();
         private int selectedType;
         void OnEnable()
         {
              behaviorTypes = ReflectionVoodooThatGetsDesiredTypes(); // this works as expected
         }
     
         void OnInspectorGUI()
         {
               if (GUILayout.Button("Add", GUILayout.ExpandWidth(true)))
         {
                     AddStuff();
                 }
         }
 
         void AddStuff()
         {
             Type childClass = behaviorTypes[selectedType];
          AddBehavior(ScriptableObject.CreateInstance<childClass >()); // this doesn't work.  what is the proper way to do what I'm conceptually trying to achieve here? 
         }
     }
     

I have a base behavior class that inherits from ScritpableObject, and then several child classes that branch off of that to create a behavior tree. I have a List that stores all types that are derived from my base behavior class. I want to be able to select a type from that list and pass it to the ScriptableObject.CreateInstance() function.

I had this previously working using this line of code:

 AddBehavior(behaviors[typeSelection].GetConstructors()[0].Invoke(new object[0]) as BaseBehavior);

this worked, but due to serialization child class types were being lost. So, now that I've switched over to ScriptableObject, my serialization problems are solved, but instantiation problems arise.

Thanks in advance

Comment
Add comment · Show 6
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 zombience · Nov 11, 2013 at 10:43 PM 0
Share

just to clarify, i do not know what type will be passed to ScriptableObject.CreateInstance, and I want that to remain flexible without having to build a large switch case, so that the behavior tree can be extended in the future with no updates to this chunk of code

avatar image jacobschellenberg · Nov 12, 2013 at 12:01 AM 0
Share

I don't think you can instantiate a scriptable object. Only $$anonymous$$onobehaviors.

avatar image zombience · Nov 12, 2013 at 12:12 AM 0
Share

ScriptableObject.CreateInstance() is the preferred method for creating a ScriptableObject. if you try to create one via:

new BehaviorObject(); // where BehaviorObject inherits from ScriptableObject

you'll get a warning.

avatar image Bunny83 · Nov 12, 2013 at 12:20 AM 0
Share

btw: UnityEngine.Object.Instantiate works for all objects derived from UnityEngine.Object (that includes even $$anonymous$$esh, $$anonymous$$aterial, ... and should also work with ScriptableObject) :)

avatar image zombience · Nov 12, 2013 at 12:26 AM 0
Share

Hmm, well, it is only a warning, but this warning pops up every time I try to use the class constructor via System.Reflection. I wanted to make sure that I do things properly to avoid any weird serialization problems later.

The warning I'm seeing:

AlternateExplode must be instantiated using the ScriptableObject.CreateInstance method ins$$anonymous$$d of new AlternateExplode. UnityEngine.ScriptableObject:.ctor()

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Nov 12, 2013 at 12:06 AM

Simply don't use the generic version of CreateInstance but the Type version

http://docs.unity3d.com/Documentation/ScriptReference/ScriptableObject.CreateInstance.html

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 zombience · Nov 12, 2013 at 12:17 AM 0
Share

how do i get this to work with a List list?

it does not work to call the method like this:

 List<Type> types; // this is how i am currently storing my BehaviorTypes, and it works for everything else that i need it for
 
 ScriptableObject.CreateInstance(types[0]); // this doesn't work

similarly, I can't just say:

ScriptableObject.CreateInstance("string_name_of_my_type"), because this doesn't actually become my inherited type, but only a scriptable object named "string_name_of_my_type".

er, what am I missing here?

avatar image zombience · Nov 12, 2013 at 12:19 AM 0
Share

I can't make an explicit reference, as it will defeat the purpose of the entire editor script.

I cannot code:

 ScriptableObject.CreateInstance<BehaviorType>();
 

because in my code, BehaviorType will in reality be any one of several subclasses of my base class BehaviorType

avatar image Bunny83 · Nov 12, 2013 at 12:28 AM 0
Share

This

     ScriptableObject.CreateInstance(types[0]);

Should work just finefine. Are you sure you have a valid ScriptableObject? So the filename matches the classname?

I can't test myself at the moment since I'm not at home and only have my tablet here :-)

avatar image zombience · Nov 12, 2013 at 12:42 AM 0
Share

arrg! haha yeah, ok, I switched some code in a different area that bypassed my typechecking, so I was no longer checking to make sure that only one BehaviorType was added. So, I was mislead to believe that ScriptableObject wasn't checking it's type, and was therefore not doing what I wanted.

Thanks for the help!

 ScriptableObject.CreateInstance(types[0]);

that works as it should. the fault is, of course, $$anonymous$$e :D

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

18 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

Related Questions

Why does the value of the public property of [Serializable] class persist in ScriptableObject between Editor runs ? 2 Answers

Cannot serialize System.Type field 3 Answers

cannot change variables on scriptableobject asset in editor 1 Answer

ScriptableObject stored in MonoBehaviour lost on quit 1 Answer

Instantiating Prefabs through Editor Script 1 Answer


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