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…
- create an empty called GameObject
- Give GameObject a MyObject component (see code below).
- Use the new Test/Run menu item (see below).
- Check in the inspector, GameObject has a MyObject with a field Mything containing a My Thing.
- Drag GameObject into the Assets window, creating a prefab GameObject.
- 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:
-
I have misread the documentation, and references to ScriptableObjects do not get
serialised? -
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?
-
There is some alternative way to get a prefab that has a reference to a ScriptableObject?
-
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>();
}
}