• 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
1
Question by yoDon · Nov 06, 2014 at 08:53 PM · webgl

Browser to Unity WebGL communication

How do we pass messages into Unity WebGL apps from browser javascript? I know the syntax for the UNITY_WEB plugin is

 u.getUnity().SendMessage("MyGameObject","MyFunction", "SomeValue");

What is the syntax for sending a message to UNITY_WEBGL from browser javascript?

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

6 Replies

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

Answer by gstier · Nov 11, 2014 at 05:38 PM

I'm using Unity 5b12. In UnityConfig.js there is a SendMessage function. The WebGL build automatically includes this in the build.

So just change:

 u.getUnity().SendMessage("MyGameObject","MyFunction", "SomeValue");

to:

 SendMessage("MyGameObject","MyFunction", "SomeValue");

Comment
Add comment · Show 2 · 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 clintomed · Jun 10, 2015 at 04:37 AM 0
Share

The Send$$anonymous$$essage function seems to now be gone from UnityConfig. Anyone have a new solution? Am I missing something?

avatar image rameshkumar · Jun 10, 2015 at 07:01 AM 0
Share

can you explain the parameter which is passing inside the "Send$$anonymous$$essage" function ,"$$anonymous$$yGameObject" * .

avatar image
1

Answer by azporispo · Jul 30, 2015 at 08:50 AM

I was able to call a function from my a gameobject in my scene from my javascript code.

My scene was loading very slow, even when I only have two gameobjects inside it. And because I called my function "SendMessage" before the page finish loading, a pop up with the error "SendMessage is not defined" appears.

-The steps to solve it were:

1-The form to call the funtion is :

function SaySomethingToUnity(_parameter_To_Send) {

SendMessage("GameObject_To_Call", "Function_To_Call", _parameter_To_Send);

}

You DO NOT need to use ""u.getUnity()" anymore.

Remember that (SendMessage will then call the function Function_To_Call() on the game object named GameObject_To_Call, passing a piece of string, int or float, data as an argument:)

2-Disable thee Exceptions in Unity(PlayerSettings->Publishing Settings->Enable Exceptions: None). This form, my scene was loading faster.

3-When everything was loaded. The "SendMessage" error did not appears anymore (the file UnityConfig.js that Unity generated when building does not have a definition for the function SendMessage, so do not look for it).

Comment
Add comment · Show 2 · 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 jrbourne · Nov 10, 2015 at 05:00 PM 0
Share

Hello: seems like a great way to make communication with Send$$anonymous$$essage work. However, I tried the things that @azposporipo and I still get the "Send$$anonymous$$essage not defined" error back. I'm running in and iFrame - unity in one iFrame and Twine in the other. I'm thinking I just don't know how to get to the javascript in the first iFrame from the second iFrame. Does anyone has a solution for that?

avatar image cchacon jrbourne · Jan 16, 2016 at 06:32 AM 0
Share

hi jrbourne, did you get this working? i'm having the same problem

thanks, Carlos

avatar image
1

Answer by HX2Chen · Dec 04, 2015 at 04:07 PM

I found the function defined of SendMessage in {YourGameProjectName}.js It's seems like this: function SendMessage(gameObject,func,param line 1,Col 536

Comment
Add comment · 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
0

Answer by cchacon · Jan 17, 2016 at 05:30 PM

I was having the error: "SendMessage not defined" when I was integrating unity3d using iframe. I switched to using webgl-template (http://docs.unity3d.com/Manual/webgl-templates.html) and added my SendMessage call there, that worked fine.

Comment
Add comment · 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
1

Answer by Schubkraft · May 20, 2016 at 02:49 PM

Hint: You need to wait for WebGL/Your game object to be loaded before calling it.

See http://forum.unity3d.com/threads/how-to-ensure-that-webgl-is-loaded-and-can-call-sendmessage.394512/

Comment
Add comment · 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
  • 1
  • 2
  • ›

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

36 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What is abort 127 0 Answers

Persistent data between different games - local save for WebGL 1 Answer

How to connect to server with a WebGL Build using Unity's Multiplayer Service, Unity 1 Answer

problem with ImageEffects in webGL 2.0 0 Answers

Is there a way to send an XMLHttpRequest and get the response in a WWW object? 0 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