Search Unity

Question How do I set up a 2D array of tiles where each tile can be selected and modified?

Discussion in 'Scripting' started by Aylith, Aug 7, 2020.

  1. Aylith

    Aylith

    Joined:
    Feb 5, 2020
    Posts:
    3
    I'm very new to scripting so not sure if this is the right place, but I understand that you can give each item in an array an index, and I believe that works on 2 different dimensions? So my question is, how would I implement that, in the most basic way possible, to create a grid of square tiles where each one can be selected and can have its properties and variables modified, including the "type" of tile? Additionally, would it be simpler to use "SelectedTile" as a GameObject variable, or as separate X and Y values to keep track of the coordinates?

    My goal is to eventually make a strategy game with custom maps that can be edited/shared by players, but right now I'm just focusing on making sure one simple flat map works.
     
    AlanMattano likes this.
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Editor Scripts can handle this for you at authoring time. There's tons of tutorials to make 2D level editors in Unity, and the tools available can be crafted however you like.

    On the other hand, if you want to make a runtime editor (what the players get to tweak), you are quickly getting into some advanced coding, as any editor will quickly become statefully complex and have to handle ALL the different ways that untrained users may attempt to use it.

    I say this from the point of many decades of software engineering experience, and I don't mean to discourage you, but be sure you understand what you are getting into with a user-facing editor. Start with an in-Unity editor, you'll learn a lot that can go into a user-facing editor should you choose to do one in the future.
     
  3. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    First create a TileScript in where you would like to hold your data for a single tile.
    Then on Start of a single script, let's say attached to the camera, you can create your GameObjects and add TileScript to them.
    Now you can access from the script that created them all of their independent data on their TileScript. Tile[x,y].GetComponent<TileScript>().PublicVariable.
    Code (CSharp):
    1. GameObject[,] Tiles = new GameObject[25,25];
    2.   for (int x = 1; x < 25; x++)
    3.         {
    4.             for (int y = 1; y < 25; y++)
    5.             {
    6.                 Tiles[x, y] = new GameObject();
    7.                 Tiles[x, y].AddComponent<TileScript>();
    8.             }
    9.         }
    10.     }
     
    Last edited: Aug 8, 2020
  4. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    Welcome to this forum!
    Here is a learning section: https://learn.unity.com/
    And here there is one of the best Unity free C# scripting courses.
    I strongly recommend these ones.
    • for beginners [LINK]
    • and intermediate [LINK]
    After you handle scripting you can try some Unity tutorials. Go to the Unity Hub and under the Learn section, you can find 2D for Beginner like Ruby's.

    Selected: probably a raycast hit will make the job?
     
    Last edited: Aug 10, 2020
    Kurt-Dekker likes this.