Keep dynamically created GameObject between scenes

I’m using DontDestroyOnLoad() to keep a GameObject added in the editor persistent between scenes but I’m at a loss as to how to keep a dynamically-created GameObject between scenes.

Here’s what I’m doing:

Scene A: A GameObject (x) added to the scene via the editor with a script that has a GameObject (y) as a variable. The script calls DontDestroyOnLoad() on the it’s parent GameObject (x) and loads mesh data from a file into the variable (y). The script then loads Scene B.

Scene B: x is preserved but the data of y isn’t.

I’ve tried calling DontDestroyOnLoad on y but it doesn’t seem to work. I know the model loading in Scene A works because I can see the data in the debugger (and I’ve tested rendering the result in Scene B without passing it from Scene A) but when I try and use y in Scene B I get this error:

Nevermind, I fixed it. I was putting the call to Object.DontDestroyOnLoad() in the object’s (not GameObject) constructor. I moved the call to a MonoBehaviour event and it worked fine.