1
\$\begingroup\$

Is it possible to do something like this?

public class SomeScriptable : ScriptableObject
{
    public MyScriptable myScriptable;

    public void OnValidate()
    {
        if (myScriptable == null)
        {
             myScriptable = ScriptableObject.CreateInstance<MyScriptable>();       
         }
    }
}

public class MyScriptable : ScriptableObject
{

    public int MyVar1;
    public string MyVar2;

}

I tried this but am getting a "Type Mismatch" error in Unity.

\$\endgroup\$
  • \$\begingroup\$ Can you clarify, are you trying to create a ScriptableObject, like your code seems to try to do, or serialize one, as the current title describes? (ie. convert a ScriptableObject to a sequential representation to write to a file or send over a network) If the latter, what do you want to serialize it into, if not an asset file? ScriptableObjects are generally thorny to serialize outside of Unity's editor functionality, so if you're headed that way it might be worth telling us more about your application - there may be simpler solutions to the root problem. \$\endgroup\$ – DMGregory Apr 27 '17 at 5:10
  • \$\begingroup\$ @DMGregory I am trying to serialize a ScriptableObject at design time without creating an asset for it. I'll have a custom inspector and the user will be able to select from certain ScriptableObjects that inherit from a certain type in the loaded assemblies. I want it to serialize as part of the Monobehaviour / ScriptableObject it's in, just like if it were a regular [Serializable] class. \$\endgroup\$ – user94720 Apr 27 '17 at 8:37
1
\$\begingroup\$

It's odd, as it is now, you should get a CS0103 compiler error, due to the fact that you need to write:

myScriptable = ScriptableObject.CreateInstance<MyScriptable>();

Moreover, if the MyScriptable class it's inside the MyComponent script attached to a game object, you'll get a warning - to get rid of it you need to cut the MyScriptable class code from it and put it in its specific script, named ofc MyScriptable.

And remember that the myScriptable instance will be serialized in edit mode as soon as you finish compiling the script for the first time, since you put CreateInstance inside OnValidate().

EDIT

You're getting the Type Mismatch error in the prefab of the game object with the MyComponent script, and that's normal, since prefabs can't get a reference to any kind of instances in the hierarchy of the scene, but only to assets.

For example, if you create a game object in the hierarchy, then attach the MyComponent script, you'll be able to access the myScriptable instance, by double-clicking on the My Scriptable field of the script in the inspector.

But, if you now create a prefab from it, you'll get the Type Mismatch error in the field of the script attached to the prefab.

And you can't really escape from it: if you want to have a reference in the prefab, you HAVE to create a myScriptable asset which can be then referenced by the prefab.

Of course you can do that by using

AssetDatabase.CreateAsset(myScriptable, "pathname+assetname.asset");

just after the CreateInstance method in MyComponent, or you can add the [CreateAssetMenu] attribute in the MyScriptable script, so you can create any asset you want in the editor, and then drag-and-drop it in the reference field of the prefab.

That's pretty much it, hope it helped.

\$\endgroup\$
  • \$\begingroup\$ Sorry, I free-typed and missed that part. The two classes are in different files and the files bear the name of their respective class name. Nothing seems to be serialized though. Instead, I get a "Type Mismatch" error in Unity, in the Object field where myScriptable is supposed to be. \$\endgroup\$ – user94720 Apr 27 '17 at 8:35
  • \$\begingroup\$ Ok, I managed to recreate/understand the "bug", bug inside the quotes because it's not really a bug. I'll edit my answer to explain the situation and how to fix it. \$\endgroup\$ – Galandil Apr 27 '17 at 12:28
  • \$\begingroup\$ Thanks for your answer but my question is about NOT creating an asset from it. \$\endgroup\$ – user94720 Apr 27 '17 at 13:16
  • \$\begingroup\$ "if you want to have a reference in the prefab, you HAVE to create a myScriptable asset which can be then referenced by the prefab." - which part of this sentence isn't clear? \$\endgroup\$ – Galandil Apr 27 '17 at 13:23
  • \$\begingroup\$ Sorry, I missed that line among your explanation on how to create assets. \$\endgroup\$ – user94720 Apr 27 '17 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy