5

I'm developing an android plugin which should start another activity (with UI and everything...). This activity (which is not inherits from UnityPlayerActivity of course) should send message to the Unity C# code. I'm trying to call UnityPlayer.UnitySendMessage, I see my log a moment before sending the message but the C# side doesn't get it (no exception is raised).

This is the callback which is called from the other activity:

@Override
public void generatePayload() {
   try {
      Log.v(TAG, "generatePayload was triggered");
      UnityPlayer.UnitySendMessage("AndroidObject", "generatePayloadMessage", "");
   } catch (Exception ex) {
      Log.e(TAG, "failed to send message to unity");
   }
}
  • I've tried to call this method from the unity primary activity and the message was received successfully in the unity side.
  • I've tried also to send the message from the other activity (as I should) but in main thread and it didn't work too...

Any suggestions? Can't I send message to unity while UnityActivity isn't in foreground? If this is the situation - what can I do?

Tal Sh
  • 111
  • 3

2 Answers2

2

there is no message/string sent in your code above only a target object and method, you are sending an empty string to generatePayLoadMessage([nothing being sent])

try

UnityPlayer.UnitySendMessage("AndroidObject", "generatePayloadMessage", "Hello world");

also please state what you have set up on the Unity side to handle the message.

urfx
  • 165
  • 1
  • 10
  • This took me way too long to solve. I fixed it and in positing my solution I found this. FYI the error from android studio is `Attempt to invoke virtual method 'byte[] java.lang.String.getBytes(java.lang.String)' on a null object reference` – Jacksonkr Mar 07 '22 at 00:48
0

if your navtive code have no parameters at all, do try to send null instead of "".

flankechen
  • 1,225
  • 16
  • 31