Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

help accessing array within multidimensional array of vector2

Discussion in 'Scripting' started by lalalannii, Sep 2, 2019.

  1. lalalannii

    lalalannii

    Joined:
    Sep 2, 2019
    Posts:
    6
    Hi all,

    I've got an array containing 5 Vector2 arrays. My goal is to shuffle the positions of the values within one of these arrays (chosen at random). I've got a shuffle method where you need to specify which vector2 array to shuffle. My issue is that I get the error message: 'Wrong number of indices inside[]; expected 2'. See code below:

    Code (CSharp):
    1.     public Vector2[,] arrayOfPositionArrays;
    2.     [SerializeField] int currentPosArray;
    3.  
    4.     private void Start()
    5.     {
    6.  
    7.         SetPositionArray();
    8.         Shuffle(arrayOfPositionArrays[currentPosArray]);   //problem here
    9.     }
    10.  
    11. public void SetPositionArray()
    12.     {
    13.         currentPosArray = Random.Range(0, 5);
    14.  
    15.         arrayOfPositionArrays[0, 0] = new Vector2(-7f, 2.53f);
    16.         arrayOfPositionArrays[0, 1] = new Vector2(-7f, -1.39f);
    17.         arrayOfPositionArrays[0, 2] = new Vector2(-4.5f, 1.34f);
    18.         arrayOfPositionArrays[0, 3] = new Vector2(-4.5f, -2.81f);
    19.         arrayOfPositionArrays[0, 4] = new Vector2(-2f, 3.95f);
    20.         arrayOfPositionArrays[0, 5] = new Vector2(-2f, -0.8f);
    21.         arrayOfPositionArrays[0, 6] = new Vector2(0.5f, 2.3f);
    22.         arrayOfPositionArrays[0, 7] = new Vector2(0.5f, -2.68f);
    23.         arrayOfPositionArrays[0, 8] = new Vector2(3.5f, 1.46f);
    24.         arrayOfPositionArrays[0, 9] = new Vector2(3.5f, -1.96f);
    25.         arrayOfPositionArrays[0, 10] = new Vector2(6.5f, 2.8f);
    26.         arrayOfPositionArrays[0, 11] = new Vector2(6.5f, -2.8f);
    27.  
    28.         arrayOfPositionArrays[1, 0] = new Vector2(-7.16f, 2.33f);
    29.         arrayOfPositionArrays[1, 1] = new Vector2(-4.4f, 1.59f);
    30.         arrayOfPositionArrays[1, 2] = new Vector2(-1.41f, 2.42f);
    31.         arrayOfPositionArrays[1, 3] = new Vector2(1.87f, 1.6f);
    32.         arrayOfPositionArrays[1, 4] = new Vector2(4.84f, 2.45f);
    33.         arrayOfPositionArrays[1, 5] = new Vector2(7.56f, 1.49f);
    34.         arrayOfPositionArrays[1, 6] = new Vector2(-7.57f, -1.9f);
    35.         arrayOfPositionArrays[1, 7] = new Vector2(-4.82f, -2.73f);
    36.         arrayOfPositionArrays[1, 8] = new Vector2(-1.81f, -2.22f);
    37.         arrayOfPositionArrays[1, 9] = new Vector2(0.95f, -2.83f);
    38.         arrayOfPositionArrays[1, 10] = new Vector2(4.01f, -2.06f);
    39.         arrayOfPositionArrays[1, 11] = new Vector2(7.06f, -2.68f);
    40.         //other 3 arrays initialized here
    41.     }
    42.  
    43.  
    44.     public void Shuffle(Vector2[] a)
    45.     {
    46.         for (int i = a.Length - 1; i > 0; i--)
    47.         {
    48.             int rnd = Random.Range(0, i + 1);
    49.             Vector2 temp = a[i];
    50.             a[i] = a[rnd];
    51.             a[rnd] = temp;
    52.         }
    53.     }
    I understand that because I have a multidimensional array, I need to specify 2 numbers. However, I want to shuffle ALL of the numbers within the one chosen array. If I specify an additional number e.g., Shuffle(arrayOfPositionArrays[currentPosArray, 0]), this will not shuffle the positions within the entire chosen array AND I also get another error message: 'Argument 1: cannot convert from 'UnityEngine.Vector2' to 'UnityEngine.Vector2[]'.

    Can someone please help me get around this issue? Many thanks!! :)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    The error is exactly what it says: you have a two-dimensional array (defined in line 1 above) and on the error line (line 8 above) you are accessing it as a one-dimensional array.
     
  3. lalalannii

    lalalannii

    Joined:
    Sep 2, 2019
    Posts:
    6
    Thanks for your reply, but my issue is how can I pass through just the one array from my two-dimensional array in my Shuffle method?
    Thanks in advance for your help!
     
  4. CurtisMcGill

    CurtisMcGill

    Joined:
    Aug 7, 2012
    Posts:
    67
    The variables arrayOfPositionArrays and currentPosArray are part of the class and you do not need to pass it as an argument. I have updated your code so it will work as described in your post.

    Code (CSharp):
    1.     public Vector2[,] arrayOfPositionArrays = new Vector2[5,12];
    2.     [SerializeField] int currentPosArray;
    3.  
    4.     private void Start()
    5.     {
    6.         SetPositionArray();
    7.         Shuffle();
    8.     }
    9.  
    10.     public void SetPositionArray()
    11.     {
    12.         currentPosArray = Random.Range(0, 5);
    13.  
    14.         arrayOfPositionArrays[0, 0] = new Vector2(-7f, 2.53f);
    15.         arrayOfPositionArrays[0, 1] = new Vector2(-7f, -1.39f);
    16.         arrayOfPositionArrays[0, 2] = new Vector2(-4.5f, 1.34f);
    17.         arrayOfPositionArrays[0, 3] = new Vector2(-4.5f, -2.81f);
    18.         arrayOfPositionArrays[0, 4] = new Vector2(-2f, 3.95f);
    19.         arrayOfPositionArrays[0, 5] = new Vector2(-2f, -0.8f);
    20.         arrayOfPositionArrays[0, 6] = new Vector2(0.5f, 2.3f);
    21.         arrayOfPositionArrays[0, 7] = new Vector2(0.5f, -2.68f);
    22.         arrayOfPositionArrays[0, 8] = new Vector2(3.5f, 1.46f);
    23.         arrayOfPositionArrays[0, 9] = new Vector2(3.5f, -1.96f);
    24.         arrayOfPositionArrays[0, 10] = new Vector2(6.5f, 2.8f);
    25.         arrayOfPositionArrays[0, 11] = new Vector2(6.5f, -2.8f);
    26.  
    27.         arrayOfPositionArrays[1, 0] = new Vector2(-7.16f, 2.33f);
    28.         arrayOfPositionArrays[1, 1] = new Vector2(-4.4f, 1.59f);
    29.         arrayOfPositionArrays[1, 2] = new Vector2(-1.41f, 2.42f);
    30.         arrayOfPositionArrays[1, 3] = new Vector2(1.87f, 1.6f);
    31.         arrayOfPositionArrays[1, 4] = new Vector2(4.84f, 2.45f);
    32.         arrayOfPositionArrays[1, 5] = new Vector2(7.56f, 1.49f);
    33.         arrayOfPositionArrays[1, 6] = new Vector2(-7.57f, -1.9f);
    34.         arrayOfPositionArrays[1, 7] = new Vector2(-4.82f, -2.73f);
    35.         arrayOfPositionArrays[1, 8] = new Vector2(-1.81f, -2.22f);
    36.         arrayOfPositionArrays[1, 9] = new Vector2(0.95f, -2.83f);
    37.         arrayOfPositionArrays[1, 10] = new Vector2(4.01f, -2.06f);
    38.         arrayOfPositionArrays[1, 11] = new Vector2(7.06f, -2.68f);
    39.         //other 3 arrays initialized here
    40.     }
    41.  
    42.     private void Shuffle()
    43.     {
    44.         for (int i = arrayOfPositionArrays.GetLength(currentPosArray) - 1; i > 0; i--)
    45.         {
    46.             int rnd = Random.Range(currentPosArray, i + 1);
    47.             Vector2 temp = arrayOfPositionArrays[currentPosArray, i];
    48.             arrayOfPositionArrays[currentPosArray, i] = arrayOfPositionArrays[currentPosArray, rnd];
    49.             arrayOfPositionArrays[currentPosArray, rnd] = temp;
    50.         }
    51.     }
     
    Last edited: Sep 2, 2019
    lalalannii likes this.
  5. lalalannii

    lalalannii

    Joined:
    Sep 2, 2019
    Posts:
    6
    Thanks for your help with this. I've now got a null reference exception error starting on line 15. How can I fix this?. Your help is greatly appreciated! :)
     
  6. CurtisMcGill

    CurtisMcGill

    Joined:
    Aug 7, 2012
    Posts:
    67
    I have updated the code to fix the problem.
     
  7. lalalannii

    lalalannii

    Joined:
    Sep 2, 2019
    Posts:
    6
    You're a legend! Thanks so much for your help, it all works!