SendMessage: object 3 does not have receiver for function ImportButtonClick!

I’m new to unity, I’m trying to call back and forth calling function of unity / javascript inbetween each other, but I’m getting a console error like SendMessage: object 3 does not have receiver for function ImportButtonClick!

C#

[DllImport("__Internal")]
private static extern void CubeComputation();

Assets/Plugins/WedGL/tests.jslib

var CubePlugin = {
  CubeComputation: function() {
    SendMessage('3', 'CubeComputation', 'shailesh');
  }
};
mergeInto(LibraryManager.library, CubePlugin);

Can you please help me for the same.
Thank you.

This is probably irrelevant but I will answer here for future references.

The SendMessage function in your jslib code should point to a function in the C# under one of the MonoBehaviours connected to the gameObject you are referring to (“3” in this case).

###For example:

C# code:

using System.Runtime.InteropServices;
using UnityEngine;

public class JSLibExample : MonoBehaviour {

    private void Start() {
        JSLibInitialize(gameObject.name);
    }

    public void JSLibCallback(int value) {
        Debug.Log("JSLib Callback! with value: " + value);
    }

    [DllImport("__Internal")]
    private static extern bool JSLibInitialize(string connectingGameObjectName);
}

JSLib:

var JSLibPlugin = {
    JSLibInitialize: function (gameObject) {
        SendMessage(gameObject, JSLibCallback, 5);
    }
};

mergeInto(LibraryManager.library, RockYouAdPlugin);

This code will execute as follows:

  1. The Start function on the MonoBehaviour calls the JSLibInitialize with the gameObject name.

  2. The JSLibInitialize function runs a SendMessage function with the gameObject name parameter which calls the function JSLibCallback on the MonoBehaviour with the value 5.


I hope this helps someone :slight_smile: