1

I have this Java Method

public class Test {
  double calculate(float[] array){
    array[0] = 2.0f;
    return 3.0f;
  }
}

I integrated this Java Method via an Android Archive into Unity. I access this method like this:

var javaObject = new AndroidJavaObject("Test");
float[] inOut = new float[1];
double res = javaObject.Call<double>("calculate", inOut);

Now, the res variable holds the value 3.0f as expected. But the inOut array does still have 0 at index position 0, but it should have 2.0f;

How do I properly pass a primitives array to Java code from Unity and use it as an "inOut" argument?

Thank you

derHugo
  • 83,094
  • 9
  • 75
  • 115
Soccertrash
  • 1,830
  • 3
  • 28
  • 48
  • Well, you should rethink your code. The float is passed as a copy, so only the copy gets modified, while the original inOut array remains unchanged. Perhaps you should create 2 methods, one that returns double and another one that returns an array. Of course I don't know what is your use case and what exactly you are trying to achieve. – Razvan S. Nov 24 '20 at 17:00
  • As far as I know both Java and C# use "Call by reference". Thus the object (= float[]) I pass to the method should not be copied but directly used. I verified this behavior with an quick test in both C# and Java. – Soccertrash Nov 24 '20 at 18:27
  • 1
    Clearly that doesn't happen in this situation. When communicating between C# and Java you are getting a copy. I'm not sure if there is a way to do what you want but there are clearly solutions to work around it. – Razvan S. Nov 25 '20 at 08:58

0 Answers0