Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jeff-Kesselman · Dec 06, 2010 at 12:05 AM · javascriptsendmessage

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(){
    NPHOS.registerListener(gameObject);
}

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

public void LoginFailed(string reason){
        Debug.Log("Login failed");
        fireEvent("OnLoginFailed",reason);  
}

...

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

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

Help?

Edit:

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>(); eventListenerDictionary.Add(evt,goList); } return goList; }

public static void AddListener(string evt,GameObject obj){ ListEventListeners(evt).Add(obj);
Debug.Log("Object "+obj.name+" 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.name);
            gobj.SendMessage(evt,value, SendMessageOptions.RequireReceiver);    
        }   
    }

It appears to be hanging or otherwise silently blowing up on gobj.name. 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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Jeff-Kesselman · Dec 06, 2010 at 01:41 AM

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

http://entitycrisis.blogspot.com/2010/08/safe-multithreading-in-unity3d.html

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Simon Wittber · Jan 11, 2011 at 02:25 AM 1
Share

Another way to do it: http://entitycrisis.blogspot.com/2011/01/asynchronous-unity3d-components.html

avatar image
0

Answer by pyro · Dec 06, 2010 at 12:11 AM

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

Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jeff-Kesselman · Dec 06, 2010 at 12:17 AM 0
Share

Ocuh. Iw as trying to avoid that as the receiver is on the app side whereas the rest is on the plugin side and I really wanted my plugin code to be app language agnostic :(

But ill try it just to prove thats the problem. If that IS the problem, its a serious Unity bug.

avatar image Jeff-Kesselman · Dec 06, 2010 at 12:29 AM 0
Share

I'm not sure if this is good news or bad news, but re-writing the receiving object in C# evidenced exactly the same behavior.

Is it possible there is something about ti$$anonymous$$g here? The login event is asynchronous, are there times during which Send$$anonymous$$essage cannot be successfully called?

avatar image pyro · Dec 06, 2010 at 12:29 AM 0
Share

ins$$anonymous$$d of using Send$$anonymous$$essage couldn't you use GetComponent() and call the function directly?

avatar image pyro · Dec 06, 2010 at 12:33 AM 0
Share

the only thing I can think of is to be 100% sure you are sending it to the right gameobject, also try using the RequireReceiver option so it will give an error if nothing picked up the message

avatar image Jeff-Kesselman · Dec 06, 2010 at 12:38 AM 0
Share

No because this is intended to be loosely coupled. When it starts receiving data there will likely be many different objects all listening. The caller wont know the names of those components.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

RayCast to send message to the object it hits? 1 Answer

Web Player SendMessage Array Parameters 1 Answer

Problem with UnityObject2.SendMessage | communicating with the web player 0 Answers

SendMessage Not Working: JavaScript 1 Answer

SendMessage script problem 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges