How to return an array on-the-fly

Hi!

Here’s how my code works:

public class myClass : MonoBehaviour{
  public static int[] myMethod(){
    List<int> aList = new List<int>();
    // ...
    // Filling the list
    int[] anArray = aList.ToArray();
    return anArray;
  }
}

and :

public class myMainClass : MonoBehaviour{
  void OnGUI() {
    Event e = Event.current;
    if (e.button == 0 && e.isMouse){
        int[] myArray = myClass.myMethod();
    }
  }
}

Now, the problem is that I need to return the array from myClass to myMainClass “on-the-fly”, that is, even if the array is not completely filed, it already has to return its first values.

Thank you!

Note: I use a list at first (and eventually convert it to an array) because of its dynamic size property.

Well It seems you just want a reference to the array in your main class. Just have the array as a public class level variable in your sub class and call the ref. But beware if the array is not instantiated it will throw an exception. Or another function or getter.