SOLVED: Problem with SendMessage -- silently failing

I have this function in a JS script attached to my object:

function OnLoginFailed(reason:String){
    Debug.Log("In on login failed");
    _messageText = reason;
    currentState = STATE.LoginFailed;

If I do a SendMessage on gameObject in my Awake it functions perfectly.

In my awake am putting a reference to the gameObject in a static list held by a C# class like this:

function Awake(){

When the NPHOS object sees this particualr event it calls this code:

public void LoginFailed(string reason){
        Debug.Log("Login failed");


    private static void fireEvent(string evt, object param = null){
        foreach (GameObject gameObject in eventListeners){
            if (param == null){
            } else {
                Debug.Log("SendingMessage "+evt+" with "+param);

I've traced through it and all that works, but the SendMessage seems to silently fail as the Javascript method never gets called.



I seme to have tracked the issue down to something very bizarre occurring in my list handling where what comes back out of the list is not the same as what went in.

Can anyone see anything wrong with this list handling code?

    private static List<GameObject> ListEventListeners(string evt){
    List<GameObject> goList=null;
    if(!eventListenerDictionary.TryGetValue(evt,out goList)){
        goList= new List<GameObject>();
    return goList;

public static void AddListener(string evt,GameObject obj){
    Debug.Log("Object "" listening for "+evt);

This is how I access the list later:

public static void Notify(string evt, object value = null){
        Debug.Log("Sending notifications for "+evt);
        List<GameObject> listeners = ListEventListeners(evt);
        Debug.Log("Listeners list length="+listeners.Count);
        foreach (GameObject gobj in listeners){
            Debug.Log("here ";
            gobj.SendMessage(evt,value, SendMessageOptions.RequireReceiver);    

It appears to be hanging or otherwise silently blowing up on Im going to put in a null check to see if somehow gobj is null...

Edit: Nope not null. This is REALLY beginning to look like a race condition. Do I need to queue my messages and only deliver them at a specific point in time or on a specific thread?

I'm pretty sure this is the answer...

I'm going to have to re-architect a bit so there is a headless object in the world that queues and handles sending events only during Update()

YUP, Thats the answer. Have it working now.

Short story is that you pretty much can't touch ANY Unity GameObject or MonoBehaviour in ANY way from another thread. You need to queue any interraction and then pick it up and deal with it in that behaviour's Update() callback.

I'm about half way to a generic framework for taking asynchronous events and turning them into Unity messages. At some point maybe I'll clean it up and post it to the wiki.

I've had some issues with Javascript picking up message sent by C#, try writing the receiver in C# too..