Attmepting to serialize my ScriptableObject with Newtonsoft.Json returns a self referencing loop error

I’m developing a modding SDK for a game which uses ScriptableObjects for ease of access to creating new attacks, then serializes them to json which is loaded in-game by a supporting MelonLoader mod; At least, it would, if I were able to serialize the objects in the first place.

Every time I try to serialize them my script returns a self referencing loop error, and I’m not entirely sure why. I’ve built a script for detecting circular references which iteratively loops over every field and property on an object and checks if it has seen that object before, which has worked perfectly in previous projects, though admittedly might not be water tight.

I’m sure there is probably a loop somewhere there, since setting the serializer to ignore loops just causes either a stack overflow or an editor crash, but my usual diagnosis tool clearly can’t find it and the error log hasn’t been helpful.

Any advice would be great; I can provide snippets of the objects I’m working with and I’m happy to provide the script for my circular reference detector if anyone wants to poke around on it.

Generally speaking you cannot serialize objects derived from UnityEngine.Object.

The most common approach for savegames is to serialize a proxy object instead.

Loading/Saving ScriptableObjects by a proxy identifier such as name:

Otherwise you would need a proxy object that you save, then have code to transfer the fields over at “injection” time.

I see; Appreciate the help, this’ll take a while but hopefully it works out. Got 40 scripts and a couple thousand lines of fields to sort through then ;-;

Unfortunately I cannot replicate your linked implementation since the scriptable objects I’m creating need to be injected into the game and are not built into it. Luckily I can mostly just copy + paste the majority of the scripts.

Why not just use plain C# objects instead? I don’t see anything in what you described that requires it to be a scriptable object.

I’m not gonna lie, I didn’t even consider that. They’re all ScriptableObjects so they can be created in the asset browser since they’re never placed on an object, but I can just use C# objects and create them using a single ScriptableObject wrapper, then serialize the class directly using a reference to the wrapper. This’ll probably save me a lot of time and tinkering, thank you!