class field assigned in awake function and refered null when calling the method in the class

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)
        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)

Ok, given the current information i simply guess you’re not calling “GetCellType” on an object in the scene but maybe on a prefab. So check the code where you’re calling that “GetCellType” and make sure the object you’re calling that method on is the actual object in the scene and not a reference to the prefab in the project. Since your “MapDemo” seems to be instantiated at runtime (due to the “Clone” in the name) you probably referencing the wrong objects.