Search code examples
javaandroidunity-game-engineaar

Calling a method with multiple arguments in AAR from within Unity3D C# script


I've got a class with the following method:

public static int add( int a, int b ){
    return a + b;
}

and I'm trying to call it from Unity Script with

var ajc = new AndroidJavaClass( "com.mil.rfcommunitylib.BluetoothClassic" );
int result = ajc.CallStatic<int,int>( "add", new int[] { 1, 2 } );

but I get

AndroidJavaException: java.lang.NoSuchMethodError at my logcat.

What's wrong? Works with methods without arguments, so we can assume I set up everything correctly.


Solution

  • Instead of calling:

    var ajc = new AndroidJavaClass( "com.mil.rfcommunitylib.BluetoothClassic" );
    int result = ajc.CallStatic<int,int>( "add", new int[] { 1, 2 } );
    

    you should actually use:

    var ajc = new AndroidJavaClass( "com.mil.rfcommunitylib.BluetoothClassic" );
    int result = ajc.CallStatic<int>( "add", 1, 2 );
    

    If you look closely in documentation: link you will see that in your situation your function returns int so it should be javaClass.CallStatic<int>(functionName, params ...) , and you pass the arguments which the function accept as separate params after the name of the function, not as an array of the same param types.