Search Unity

Mixing the matrix.

Discussion in 'Scripting' started by gurkan, Jun 30, 2020.

  1. gurkan

    gurkan

    Joined:
    Jul 4, 2013
    Posts:
    23
    Hi,
    My game has a gameobject [,] that I want to mix.

    This code is working only array:

    Code (CSharp):
    1.  public static void ShuffleArray<T>(T[] arr) {
    2.    for (int i = arr.Length - 1; i > 0; i--) {
    3.      int r = Random.Range(0, i);
    4.      T tmp = arr[i];
    5.      arr[i] = arr[r];
    6.      arr[r] = tmp;
    7.    }
    8. }
    but I use matrix. how do i do this..
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    You can do this two ways:

    1. copy the 2-dimensional GameObject array into a temporary one-dimension array, use the above shuffle routine, and then copy it back to the 2-dimensional array

    OR

    2. change the above to iterate i and j (nested for loops) and choose an r (and perhaps an r2) that satisfies the necessary "don't pick beyond where I have iterated yet" rule.

    Probably easiest to do #1 honestly.