Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question I want to create a 2D array of Transforms

Discussion in 'Scripting' started by MqcCheeze, Dec 6, 2022.

  1. MqcCheeze

    MqcCheeze

    Joined:
    Jun 21, 2021
    Posts:
    16
    How would I go about creating a 2D array of Transforms.

    upload_2022-12-6_15-17-33.png

    Simply doing this doesn't allow me to drag and drop any gameObjects into the array as it is a 2D array.

    So I do what I've coded below instead

    View attachment 1161969

    There's something I'm not getting and I don't know what it is.

    I just want to create a 8x8 grid as an array for a Chess game in which:

    piece.transform = boardPos[2,4];

    which will move the piece's location to that of the gameObject in position 2,4

    upload_2022-12-6_15-38-10.png

    This is another solution that I thought would work which is by setting the value in boardPos to the index within the index of xPos which is an array of the "y positions". But this doesn't seem to work as it outputs the error "Cannot implicitly convert type 'UnityEngine.Transform[]' to 'UnityEngine.Transform"


    How do I assign gameObjects to every index in the 2d array without referencing every single gameObject since I can't drag and drop
     

    Attached Files:

    Last edited: Dec 6, 2022
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Last edited: Dec 6, 2022
  3. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    655
    You need a
    Transform[][]
    (an array of arrays or a jagged array like @Brathnann said) which items can be accessed by
    xPos[i][j]
     
    Bunny83 likes this.
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,919
    Just to make that a bit more clear in general. Arrays always have a certain element type. The element type is always to the left of the square brackets So
    Transform[]
    is an array that contains Transforms. On the other hand
    Transform[][]
    is an array which element type is an array of Transforms. So the type nesting goes from right to left for arrays. Accessing the different "layer" on the other hand works the other way round. So when you index a jagged / nested array like this
    Transform[][] xPos;
    , the first index gives you an element of the outer array. So you get a
    Transform[]
    back

    Code (CSharp):
    1. Transform[] sub = xPos[5];
    2. Transform t = sub[2];
    This is the same thing just in one line:
    Code (CSharp):
    1. Transform t = xPos[5][2];
    Types can get really confusing, but once you understand how the array syntax work, it's pretty easy. You can even create crazy types like

    Code (CSharp):
    1. private List<int[]>[] myArray;
    This is an array of a generic List that in turn contains arrays of integers. Just keep in mind that array are objects themselfs and need to be created at some point. Things that are serializable in the inspector can get initialized through the inspector itself. However Unity does only support one dimensional arrays and does not support jagged arrays. Though it does support arrays of serializable objects which also contain an array. So you get a nested array, but uglier :). At least it can directly be serialized and displayed in the inspector.
     
    Nad_B likes this.