- Home /
How do I call a Unity function from HTML using JavaScript?
I have my Unity web player embedded in an HTML document online. How would I call a function within Unity, using Javascript in the HTML?
For example, I click a button in the HTML and a function within Unity is called.
I know the opposite of what I'm trying to do is Application.ExternalCall,, but how do I call functions from Unity?
Answer by tonycoculuzzi · Aug 01, 2012 at 06:24 AM
To answer it myself: You have the function in a Monobehaviour, and have the monobehaviour in the scene, and use:
var unity = GetUnity();
unity.SendMessage("ObjectWithFunctionToCall", "FunctionName", paramsToSend);
Be careful with the type of argument you pass, as far as I remember the argument is converted to a string in the end. So on the receiver side (in Unity) you should expect a string from javascript. Correct me if I'm wrong - havn't used that for a while and maybe the API was updated.
I've had success in passing an int without having to do any converting or parsing, so I'm assu$$anonymous$$g it can send basic data types, but I'm still not 100% sure.
hmm have been looking for this same thing, so let me get this straight..."You have the function in a $$anonymous$$onobehaviour" refers to a funtion inside unity that you wish to call, right? and the code below it is code on your html page, right? got a bit confused, so correct me if I'm wrong!:D
Yep, make sure the script in Unity that you want called is on a game object in the scene and you're set.
Your answer
![](https://koobas.hobune.stream/wayback/20220613074707im_/https://answers.unity.com/themes/thub/images/avi.jpg)