ScriptableObjects in a raw-class and Instantiate.

Hi,

firstofall sorry if someone already asked it. Can’t find it myself. Hope you don’t mind.

So. I made a class ,here is what it looks like.

[System.Serializable]
public class NoMonoBehaviour

{
    [SerializeField] private MyScriptableObject _scriptableObject;
}

As you can see it has been serialized, and has scriptable object as serialized field.

Yet I have another class with monobehaviour.

public class MonoBehaviourClass: MonoBehaviour
{
     [SerializeField] private NoMonoBehaviour _classWithFieldOfScriptableObjects;
}

I add MonoBehaviourClass to the prefab. Put prefab in resources folder.

Now in Inspector I can expand the ClassWithFieldOfScriptableObjects and it shows the _scriptableObject field.
I create new asset of MyScriptableObject type, wich naturally derives from ScriptableObject class.
And set the created MyScriptableObject to _scriptableObject field.

Unfortunately when I make PhotnNetwork.Instantiate(prefab); it creates prefab.name(clone) but the _scriptableObject field is empty.

Thanks if someone can tell me how to make this work, or at least why it is not working as I wanted to.

I am pretty confident that most Unity engine objects cannot just be serialized wholesale, as they don’t really live fully in C# land but rather as a hybrid of native Unity engine data and C# accessors. Therefore this line:

[SerializeField] private MyScriptableObject _scriptableObject;

is not going to be useful in the way you think it is.

Within MonoBehaviors and ScriptableObjects, public references to assets on disk (or assets in the same relative prefab) will be serialized. An entire raw ScriptableObject somewhere else does not get the same treatment.