Get name of Array[,]

For debugging purposes, to know what is going on I’d like to see what the Array the Method is using is called.

In short.

First step:

public Vector3[,] myArray_A;
public Vector3[,] myArray_B;
public Vector3[,] myArray_C;
public Vector3[,] myArray_D;

myArray_A = new Vector3[6,6];
myArray_B = new Vector3[6,6];
myArray_C = new Vector3[6,6];
myArray_D = new Vector3[6,6];

Then after these arrays went through a few methods doing stuff with it,ultimately deciding wich of the 4 arrays it is going to use to store a Vector3.

public void MyMethod(Vector3 someLocation, Vector3[,] theChosenArray)
{
Debug.Log("Found location " + someLocation + " moving location to: " + theChosenArray);
}

In the console it only shows “UnityEnginge Vector3{,]” I can understand why that is. But how do I get the by me defined name ‘myArray_A’ or ‘myArray_B’ etc. to show in the console?

It is unfortunately not possible to get the name of the field from which the array was originally fetched. You’ll need to somehow manually get the field name and pass it to MyMethod if you need it.

As one possible solution, if you replaced Vector3 with your own class, you could have it contain a name in addition to the Vector3 data.

1 Like

This is called a wrapper, if @BenVenNL wants to read up on it. An overwhelming majority of the time, doing this has almost always only benefits.

1 Like

Yes, and to add to that - depending on what “MyMethod” does, it might be more appropriate to make it a member function of your wrapper. Thereby making it unecessary to even identify which array it is.

Note that in the fully general case, the question you are asking doesn’t have any well-defined answer, so the language can’t automatically provide this functionality even if it wanted to. Consider that you could do things like:

Vector3[,] myArray_A = new Vector3[6,6];
Vector3[,] myArray_B = myArray_A;
Vector3[,] myArray_C = myArray_B;
// You have 3 different variables, but only 1 vector shared between them
// myArray_A, myArray_B, and myArray_C are all just "handles"
// for the same object

myMethod(someLocation, new Vector3[6,6]);
// The array passed to the method in this invocation isn't
// stored in ANY variable--the method itself has the
// only reference to it!

I don’t exactly understand why you want this, but I might do it with a list of arrays and an enum which correlates to the indexes. (haven’t tested this, just writing it into the forums)

public Vector3[,] myArray_A;
public Vector3[,] myArray_B;
public Vector3[,] myArray_C;
public Vector3[,] myArray_D;

public enum MyArrayEnum {myArray_A, myArray_B, myArray_C, myArray_D};

private List<Vector3[,]> myArrayList = new List<Vector3[,]>();

void Start()
{
    myArray_A = new Vector3[6,6];
    myArray_B = new Vector3[6,6];
    myArray_C = new Vector3[6,6];
    myArray_D = new Vector3[6,6];

    myArrayList.Add(myArray_A);
    myArrayList.Add(myArray_B);
    myArrayList.Add(myArray_C);
    myArrayList.Add(myArray_D);
}

public void MyMethod(Vector3 someLocation, Vector3[,] theChosenArray)
{
    string arrayName = "not found";
    int index = myArrayList.IndexOf(theChosenArray);
    if (index >= 0)
    {
        MyArrayEnum mae = (MyArrayEnum)index;
        arrayName = mae.ToString();
    }

    Debug.Log("Found location " + someLocation + "  moving location to: " + arrayName);
}