Problem making a grid for a TBS game

The way im making the grid at the moment is using a very simple script attached to a bunch of game objects (each representing a tile).

 public class GridTile : MonoBehaviour {
        public GridTile north;
        public GridTile south;
        public GridTile west;
        public GridTile east;
        public int defB onus;
        public int movCost;
        public bool isAccessible = true;
    }

I was wondering if anyone knows of a better solution, maybe something using planes. Because as it stands, its pretty costy to load 40+game objects per stage.
Thanks in advance.

What you are doing is perfectly legitimate and fine. Loading 40 objects to a scene is a piece of cake for Unity. When you are building a board game, each tile should be an object of its own, as you are trying to do.

If you see any performance problem, it is not because of the quantity, but probably because of the quality of code.

Make sure that your objects are as light as possible:

  • in terms of loading time (whats in your Start() and Awake())
  • in terns of memory/drawcalls
  • and in terms of what it does in its Update()

For further optimization (although I would recommend against premature optimization), you can consider deactivating some tiles if they are hidden from view, and/or replacing Update() with your own less frequent game loop (with StartCoroutine())

Finally, if you have a specific reason why you think what you are doing is wrong, please be more specific.

Could be out of topic, but consider to have an enumeration of directions (the four north-south-ecc and maybe also the other diagonals) and have an array of GridTile pointers. In this way your algorithms would be more easy to be written. For example, the “turnBack180Degrees” could be (in Java-like, i don’t
know how C#'s enumerations works) :

enum Directions {
    North(0,1), South(0,-1), West(-1,0), East(1,0), N_E(1,1), S_W(-1,-1). N_W(1,-1), S_E (-1,1);
    //add stuffs useful for a matrix representation of your grid of GridTile
    final int xOffset, zOffset;
    Directions( int xo, int zo){
        xOffset = xo;
        zOffset = zo;
    }
}
public static final Directions[] valuesDirections = Directions.values();

public static Directions turnBack180Degrees( Directions dir ){
    int ordinal;
    ordinal = dir.ordinal();
    return valuesDirections[ ((ordinal & 0x1) == 0) ? (ordinal +1) : (ordinal-1)  ];
}

Using that enumeration (and its array of values) you can iterate over all of GridTile’s neighbour (this is usefull on pathfinding algorithms).

public static GridTile getNeighbour( GridTile[][] matrix, GridTile node, Direction dir ){
     return matrix[ node.z + dir.zOffset ][ node.x + dir.xOffset ];
}

Hope it helps!