IL2CPP does not support marshaling delegates that point to instance methods to native code.

I’m encountering an issue when I try to call RegisterDidChangeTagsSetCallback from my game. I get the following error in xCode: “IL2CPP does not support marshaling delegates that point to instance methods to native code.” This game is also only for iOS, so I haven’t tested it on any other platform.

I found a similar issue on Unity Answers, but the solution seemed to be to make the callback method static, which mine already was. They also seemed to have the [MonoPInvokeCallback(typeof(TagsChangedDelegate))] line above the method, which I added, but to no avail.

I’m not experienced with anything in this realm, so any help would be greatly appreciated!

C++ Code:

extern "C" {
typedef void (*TagsChangedDelegateCallback)(const char *oldTagsDate, char *oldTags[], const char *newTagsDate, char *newTags[]);
void registerDidChangeTagsSetCallback(TagsChangedDelegateCallback callback);


C# Code:

    private static extern void registerDidChangeTagsSetCallback(TagsChangedDelegate callback);

    public delegate void TagsChangedDelegate(string oldTagsDate, string[] oldTags, string newTagsDate, string[] newTags);

    public static void RegisterDidChangeTagsSetCallback(TagsChangedDelegate callback)
        if (Application.platform == RuntimePlatform.IPhonePlayer)

Where I’m calling the code:

    public void ListenForTags()
        if (!isRegistered)
            isRegistered = true;
            LXProxSeeSDKBridge.TagsChangedDelegate tagsChangedDelegate = Something;

    void Something(string oldTagsDate, string[] oldTags, string newTagsDate, string[] newTags)
        if (newTags.Length > 0)
            proxSeeTMP.text = proxSeeTMP.text + "

" + newTags[0];

I think the other issue you are missing here is that the method Something in the third code snippet needs to be a static method. Also, Something needs to have the [MonoPInvokeCallback(typeof(TagsChangedDelegate))] attribute. I don’t believe that RegisterDidChangeTagsSetCallback needs to have that attribute.

Unfortunately the error message that IL2CPP reports is not great here, since it does not mention the name of the method which needs to be static and have the attribute. We’ve improved the error message to include that information in newer versions of Unity - so that will help in the future.

Also note that marshaling of string arrays from native to managed code is not trivial - there be dragons. I’d recommend investigating the arguments passed to Something to make sure they are correct in all cases.