References in hierarchy object become null in prefab (I've followed previous posts but it still does not work)

I have looked at all the posts that I can find, and I think I am doing everything I am supposed to, but it still is not working…

  1. create an empty called GameObject
  2. Give GameObject a MyObject component (see code below).
  3. Use the new Test/Run menu item (see below).
  4. Check in the inspector, GameObject has a MyObject with a field Mything containing a My Thing.
  5. Drag GameObject into the Assets window, creating a prefab GameObject.
  6. Look at the prefab GameObject in the inspector → field Mything is None.

What I want of course is for the prefab to have a My Thing just the same as the object in the hierarchy.

So what am I doing wrong?

I realise that there have been numerous posts here with similar questions, but I thought I had followed all the advice there, and it is still not working.

As I read the documentation, it is possible to serialise references to ScriptableObjects. Also, that making a prefab was just serialising the object. So I thought that MyObject.mything (containing a MyThing) would be serialised and would appear in my prefab containing a MyThing . So, which is the case:

  1. I have misread the documentation, and references to ScriptableObjects do not get
    serialised?

  2. I have made some coding error, and if I fix my code, the MyObject will appear in the prefab? In which case, what change do I need to my code?

  3. There is some alternative way to get a prefab that has a reference to a ScriptableObject?

  4. Or what?

The ScriptableObject only needs to be there when I instantiate the prefab; the prefab could just have an embedded serialised ScriptableObject, as long as it gets instantiated and deserialised when I instantiate the main prefab.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MyObject : MonoBehaviour {
  public MyThing mything;
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MyThing : ScriptableObject {
  public string someData;
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class MakeThing : MonoBehaviour {

  [MenuItem("Test/Run")]
  public static  void doIt() {
    GameObject go = GameObject.Find("GameObject");
    go.GetComponent<MyObject>().mything = ScriptableObject.CreateInstance <MyThing>();
  }
}
  • prefabs can hold prefab/ assets references.
  • scene gameobjects can hold prefab/ assets and scene gameobject references

That’s it, the rest needs to be linked programmatically