Hi,
I am having difficulty setting objectReferenceValue in an Editor, what am I doing wrong?
public class TestObj : ScriptableObject
{
}
public class TestBehaviour : MonoBehaviour
{
public TestObj obj = ScriptableObject.CreateInstance<TestObj>();
}
[CustomEditor(typeof(TestBehaviour))]
public class TestEditor : UnityEditor.Editor
{
public SerializedProperty testObj;
void OnEnable()
{
testObj = serializedObject.FindProperty("obj");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
if (GUILayout.Button("Output"))
{
Debug.Log(testObj.objectReferenceValue);
}
if (GUILayout.Button("Set"))
{
testObj.objectReferenceValue = ScriptableObject.CreateInstance<TestObj>();
}
serializedObject.ApplyModifiedProperties();
}
}
Clicking “Output” the first time logs “(TestObj)”
Then clicking “Set” and “Output” then logs “Null”
Its as if the objectReference isnt being set.
NOTE: I am doing this on a prefab…
What am I doing wrong?