Object in ScriptableObject gets deletet when starting program.

Hello,
I have a ScriptableObject

public class MyScriptableObject : ScriptableObject
{
    public bool test;
    public object test2;
}

in which a “bool” and an “object” are stored. I have the following code in the custom editor for the object:

myScriptableObject.test = EditorGUILayout.Toggle("test", myScriptableObject.test);
myScriptableObject.test2 = EditorGUILayout.Toggle("Test2", myScriptableObject.test2 != null) ? new object() : null;

So I basically use this variables just to show 2 buttons. I can use both buttons in Unity, but as soon as the program is started, the 2nd button is reset (the one, that uses the object to store the data). The only thing I found about it is here. It would be great if someone knows how to make unity not delete the object.

Your second editor code line is “toggling” a UnityObject by observing it as a bool, then either nulling it or creating a brand new C# object generic base class instance… what does that even mean?

Wouldn’t you just drag the object in you want?

Oh, that was a copy error. But i tested both variants and neither worked. The main problem is that every type of the ScriptableObject is set to null since i start the program.

Because the object doesn’t extend from MonoBehaviour and is created by my code, I can’t drag it in.
However, I believe that the error does not happen if I let my class also inherit from ScriptableObject, then the error no longer occurs and I can also simply drag the object into the Unity editor. So this should solve the Problem.

Perfect. It now works exactly how I wanted it to.

Just an FYI, if you want to serialise anything into a System.object field the you need to use SerializeReference.