I have a problem when using this class https://forum.unity3d.com/threads/is-filesystemwatcher-on-windows-currently-broken.180251/ It looks good since it is called when something in the observed dir happen. Anyway if I try to use GetComponent for accessing components of the GameObject, or trying to use some unity api, (Find or other stuff) or if I try to create gameobjects in the start function, when I come in the OnChange callback, everything gets corrupted. I've also try to pass an object through the handler definig in this way: var handler = new System.IO.FileSystemEventHandler((s, e) => OnChanged(s, e, self)); watcher.Changed += handler; where self is the object with this script attached and it is valid during Start, when the watcher is instatiated and this lines are executed. Why?
As the linked question states in the very last reply, the file system watcher events happen on a different thread from the main thread. This means you can't do any Unity API calls within the handler. You'll have to set up a flag or a queue or something to communicate the event to the main thread. Edit: Here is some discussion on setting up such a queue for inter-thread communication: https://forum.unity3d.com/threads/multi-threaded-usage-of-unity-api.348072/