1

I am using Unity WebGL for my project and want to communicate from browser JavaScript to C#. One way is to call C# function from JavaScript using SendMessage. I am easily making call using this code snippet in javascript:

gameInstance.SendMessage("MyObjectName","MyFunctionName",myParameter);

But there is a problem it is not returning any value. For getting a value I have to implement a separate function that run another function. Can I return value directly? Send Message docs clearly stating it don’ t return any value, so other than sendMessage is there any way available to get return value from directly function calling?

5
  • Maybe this one can help docs.unity3d.com/Manual/…
    – caxapexac
    Feb 28 '19 at 8:34
  • Yeah, I have already studied it but its didn't include return value related things. Feb 28 '19 at 9:08
  • You can try to change any property in myParameter to get info about something <thinking>
    – caxapexac
    Feb 28 '19 at 9:29
  • ?? How myParameter is passed to function for input not for return value. Feb 28 '19 at 9:39
  • Your parameter can be reference type. If you wanna just return any info about external process - pull this info into this class
    – caxapexac
    Feb 28 '19 at 9:41
0

Use your custom class as a parameter - for example:

public class CustomData
{
    public string Input;
    public string Output;
}

...

CustomData data = new CustomData();
data.Input = "Something";
gameInstance.SendMessage("MyObjectName", "MyFunctionName", data);
//use data.Input inside MyFunctionName like a parameter
//change data.Output inside MyFunctionName
//use data.Output
Debug.Log(data.Output);
2
  • Are sure that it will work? i am using this line in javascrip side, not c#. gameInstance.SendMessage("MyObjectName", "MyFunctionName", data); Feb 28 '19 at 10:27
  • Not sure, just try it out)
    – caxapexac
    Mar 1 '19 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.