Here is my code looks like
public class GridData : MonoBehaviour
{
private Tilemap grid;
private void Awake()
{
grid = GetComponent<Tilemap>();
}
public GameDefinition.TerrainType GetCellType(Vector3Int position)
{
Debug.Log(grid);
return GameDefinition.TerrainInitialization.GetTerrainType(grid.GetTile(position).name);
}
}
And in the GetCellType method, i got null in the grid. What happened?
Sorry for not detailed.
This GetCellType method is called in an update function in another script.
When i debugging, the getcomponent successfully return a TileMap obj. The ‘grid’ is assigned properly. Certianly not
null. The ‘grid’ field is private, so definitely i dont have any chance to set ‘grid’ to be null outside the script.
Here is the error information from unity
UnassignedReferenceException: The variable grid of GridData has not been assigned.
You probably need to assign the grid variable of the GridData script in the inspector.
UnityEngine.Tilemaps.Tilemap.GetTileAsset (UnityEngine.Vector3Int position) <0x3b49bc10 + 0x0005a> in <0055bb3f15254c11b9bd2f2417507136>:0
UnityEngine.Tilemaps.Tilemap.GetTile (UnityEngine.Vector3Int position) (at C:/buildslave/unity/build/Modules/Tilemap/ScriptBindings/Tilemap.bindings.cs:107)
GridData.GetCellType (UnityEngine.Vector3Int position) (at Assets/GridData.cs:18)
MapData.CellAccessGet (UnityEngine.Vector3Int position) (at Assets/MapData.cs:38)
UIUpdator.Update () (at Assets/1_Scipts/6_UI/UIUpdator.cs:37)