I am trying to send a message to my Unity3D WebGl app.
The controller of my GameObject "Player" looks basically like this:
public class color : MonoBehaviour {
void Start ()
{
gameObject.GetComponent<Renderer>().material.color = Color.red;
}
public void green(string s)
{
gameObject.GetComponent<Renderer>().material.color = Color.green;
}
}
and my Javascript method like this:
function SaySomethingToUnity() {
SendMessage("Player", "green", "teeeext");
}
But when I call it nothing happens. What am I doing wrong?
I am trying to send a message to my Unity3D WebGl app.
The controller of my GameObject "Player" looks basically like this:
public class color : MonoBehaviour {
void Start ()
{
gameObject.GetComponent<Renderer>().material.color = Color.red;
}
public void green(string s)
{
gameObject.GetComponent<Renderer>().material.color = Color.green;
}
}
and my Javascript method like this:
function SaySomethingToUnity() {
SendMessage("Player", "green", "teeeext");
}
But when I call it nothing happens. What am I doing wrong?
Share Improve this question asked Jun 15, 2016 at 11:13 IpomeaIpomea 1513 silver badges16 bronze badges 11- What browser are you testing this in? Could be a potential Security Sandbox Violation if you are testing locally. Firefox sometimes lets this slide, but not always. This Violation usually fails silently as well. – Zze Commented Jun 15, 2016 at 12:51
-
When you call
SaySomethingToUnity()
in your browser, can you use the Console (F12) and tell us if there are any messages? – Alisson Reinaldo Silva Commented Jun 15, 2016 at 14:43 - I am using firefx right now beacuse on chrome i can`t test it without a server as far as I understood. In the beginning i had this error: Invoking error handler due to ReferenceError: SendMessage is not defined But its gone now. because I read somehwere that it only works if i put "Enable Exception" in Publishing Settings to none. – Ipomea Commented Jun 15, 2016 at 15:17
-
How and when do you call
SaySomethingToUnity
function? – Programmer Commented Jun 15, 2016 at 17:32 - I call it like this: <script type="text/javascript"> SaySomethingToUnity(); </script> at the end of my body in the html – Ipomea Commented Jun 15, 2016 at 17:56
2 Answers
Reset to default 7Decided to post the ment answer as the answer for people that will have this problem in the future.
The page must finish loading before calling any function from Unity or else it won't work. When Unity is not yet done loading, the Objects
that you want to send message to does not exist. Error will be thrown because of this or it will silently fail if Exception
is disabled. So you must wait for some time before calling Unity function from Unity.
I'd remend to put this option inside your own game, but if you really need to make an external script/app (like some website hosted somewhere) talk to unity, it should be done via an API.
Create an API with your preferred backend (PHP, ASP.NET WebAPI, Node.JS), expose a method ReceiveMessageFromExternalSource()
so you can call from your javascript using a post request (using proper security), store it somewhere (either a database or any other persistance engine), then use your Unit3D app to call this same API using WWWForm()
, to get said value. To avoid calling the server from Unity all the time looking for updated values, and make things work in "real time", I'd suggest you study things like WebSocket to make your server send a Signal to Unity, or even implementing a small server with Unity.