Play mode data is different

I have been working on a node based AI system for a game. It works reasonably well but I’ve run into a curious bug I can’t figure out. (Image below for reference.)

The inspector is looking at the connection between the first two nodes. Currently it has a Story trigger. When I enter play mode I get the below image.

The first node is active because it’s in play mode, but everything is working as expected. So then suppose I want to add a timer to the connection.

So I go back to edit mode, add the timer and everything looks good, but when I go back to play mode…

The timer element is gone.

As near as I can tell, whatever data I put on a new element gets saved the first time I go into play mode, but then it keeps that data permanently. No matter what changes I make in edit mode after that it goes back to the data that was there the first time I went into play mode.

I can delete the connection, re-create it, add both elements (story and time), go into play mode, and everything is fine. Which works as a work-around, but doesn’t explain what’s going on.

Any changes made in edit mode are persistent to edit mode. I can save, close, open, etc. and the data is fine. So it’s clearly being serialized and saved. It is also there after I come back out of play mode. But whenever I go into play mode the object is stuck with only the data that was present when play mode was first activated for that object.

I haven’t seen it clearly documented anywhere, but I assume Unity makes a copy of everything before entering play mode so that when you leave play mode everything reverts to the original. Is it possible it doesn’t make a clean copy each time and is retaining old data? I deleted the Library folder so Unity would re-import everything in case there was data hiding in there, but that didn’t help. I also removed the Collaborate data in case that was contributing, but it doesn’t seem to be.

Obviously there’s a lot of custom editor code making this work. I don’t have a ton of experience with editor code, so I could easily be doing something wrong, but the fact that everything is working in edit mode suggests to me that the serialization is all working properly. I’ve been trying to figure out what would cause it to be different in play mode, but I can’t come up with anything, so I’m hoping someone here has some ideas.

I’ve been prototyping and testing the AI editor so the code is a bit of a mess and intertwined with the rest of the game I’m making, so I can’t easily just extract this code and post it. I can try and trim down a project if looking at the actual code would be useful, but I feel like I’m missing something fundamental about how play mode works.

Unity caches many resource files in it’s AssetDatabase, and does not automatically detect changes for certain filetypes, such as text files. I imagine your data is cached. To fix this, whenever you modify that data, call UnityEditor.AssetDatabase.Refresh();. Because this is a function of the UnityEditor namespace, you can only call this function in editor code, it will not compile for standalone builds.

Well just in case someone else comes across this there were no issues with the editor code or serialization. Also refreshing the asset database had no effect. I had different bits of code keeping copies of the same variable in three different places which is why I saw different data in different cases. Just bad design on my part.