- Home /
Trouble editing scriptable tiles in Unity Editor
Greetings,
I am making a tile-based game akin to Advance Wars, where terrain features hinder the movement of different units to different extents (e.g. the movement cost of a forest tile is 2 for ground units, but only 1 for air units). I use A* pathfinding for my project.
My plan is as follows:
The tiles should "know" their movement costs for each unit type.
The pathfinding algorithm should "know" what type of unit is accessing it; it then "asks" the tiles what their movement costs should be while exploring neibouring tiles to find a shortest path for the unit.
Here is the realization of 1, using custom, scriptable tiles:
[CreateAssetMenu(fileName = "New GameTile", menuName = "Tiles/GameTile")]
public class GameTile : Tile
{
public int[] movementCostReference = new int[3]; //(cost_ground, cost_amphibious, cost_air)
//Other properties
}
Here's the realization of 2, a part of the A* pathfinding algorithm:
private void ExamineNeighbours(int movementType, List<GameTile> neighbours, GameTile current)
{
for(int i = 0; i < neighbours.Count; i++)
{
GameTile neighbour = neighbours[i];
/*neighbour is from GameTile class; it "knows" its movement costs corresponding to different unit types*/
int G = neighbour.movementCostReference[movementType];
//Proceed to calculate G, H, F values used for determining shortest path
}
}
I believe the logic is correct, but when I assign movement costs to the scriptable tiles in Unity Editor, use SetTile method to instantiate the scriptable tiles, then run the pathfinding, the movement cost is 0 for all tiles and all unit types. It seems like the values assigned via the editor are simply ignored, leaving everything 0 by default.
As an experiment, I tried adding a "public int test = 3" variable; accessing the variable during runtime always returned 3, even if I gave it different values in the editor (as in the above screenshot).
What is the problem here? Should I not use SetTile to instantiate custom tiles? Should I not use scriptable objects as tiles? Or am I just missing something else?
Your answer
