title: Unity3D, how to process events in the correct thread
body: <p>I'm writing a Unity3D script and using a networking library. The library emits events (calls delegates) when data is ready. My library reads that data and emits events which try to access <code>GameObject</code> but I get these errors</p>
  
  <pre><code>CompareBaseObjectsInternal can only be called from the main thread.
  Constructors and field initializers will be executed from the loading thread 
  when loading a scene. Don't use this function in the constructor or field 
  initializers, instead move initialization code to the Awake or Start function.
  
  ArgumentException: CompareBaseObjectsInternal can only be called from the main 
  thread. Constructors and field initializers will be executed from the loading 
  thread when loading a scene. Don't use this function in the constructor or field 
  initializers, instead move initialization code to the Awake or Start function.
  </code></pre>
  
  <p>That's fine. I think I understand that the network messages are being handled on a separate thread and Unity3D is trying to point that out and how it's not thread safe to go manipulating stuff from another thread.</p>
  
  <p>My question is: What's the recommended way to deal with this?</p>
  
  <p>What I'm thinking is instead of calling the delegate directly in my handler I'll make some kind of object that references all the parameters delivered in the event and adds them to a <code>List&lt;MyEvent&gt;</code> of events. </p>
  
  <p>I'll have to make a thread safe wrapper class to contain that list that lets me add and remove events with a lock because <code>List&lt;&gt;</code> is not thread safe. </p>
  
  <p>I'll then somehow need to process those events on the main thread. I'm not sure what's the best way to do that. The class that's dealing with the network is a <code>MonoBehavior</code> itself so I guess I can have its <code>Update</code> method remove events from the list and send them.</p>
  
  <p>Does this sound like a good idea or is there some other, better, or simpler way to achieve this?</p>