How can I get my Custom Tile from my tileMap

I have created a custom tile that inherits from Tile. I need my tiles to store several variables and thought this would be the best way:

    using UnityEngine;
    using UnityEngine.Tilemaps;
    [CreateAssetMenu(fileName = "New AStarTile", menuName = "Tiles/AStarTile")]
    public class AStarTile : Tile
    {
        public TileScriptableObject tileScriptableObject;
        public Sprite litSide;//Tile image that should be shown when lit
        public Sprite darkSide;//Tile image that should be shown when dark
        public bool currentlyLit = false;//Whether the tile has been flipped
    }

I want to get one of these custom AStarTiles from my tileMap and change its sprite from its stored darkSide sprite, to its litSide sprite. Focus on the ChangeTile function, at the bottom.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

public enum TileType {ENTRANCE, EVENT, AMBUSH, CRYSTAL, TREASUREROOM }

public class Astar : MonoBehaviour
{
    private TileType tileType;

    [SerializeField]
    private Tilemap tileMap;

    [SerializeField]
    private AStarTile[] tiles;

    //[SerializeField]
    //private RuleTile entrance;

    [SerializeField]
    private Camera camera;

    [SerializeField]
    private LayerMask layerMask;

    private TileScriptableObject tileScriptableObject;//current tiles scriptable object


    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit2D hit = Physics2D.Raycast(camera.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, Mathf.Infinity, layerMask);

            //if (hit.collider == null)
            //{
                Vector3 mouseWorldPos = camera.ScreenToWorldPoint(Input.mousePosition);
                Vector3Int clickPos = tileMap.WorldToCell(mouseWorldPos);
                ChangeTile(clickPos);
           // }
        }
    }

    private void ChangeTile(Vector3Int clickPos)//used to flip tile to other side
    {
         AStarTile selectedTile = tileMap.GetTile(clickPos);


        if (selectedTile.currentlyLit == false)//if the tile is not lit
        {
            selectedTile.currentlyLit = true;
            selectedTile.SetSprite(selectedTile.litSide);

            //tileMap.SetTile(clickPos, tiles[0]);
        }

    }
}

The issue I’m having is this line:

 AStarTile selectedTile = tileMap.GetTile(clickPos);

I’m getting the warning: “Assets\Scripts\Astar.cs(48,35): error CS0266: Cannot implicitly convert type ‘UnityEngine.Tilemaps.TileBase’ to ‘AStarTile’. An explicit conversion exists (are you missing a cast?)”

This doesnt make sense to me because my tile inherits from the Tile class. If i change the line to:

 TileBase selectedTile = tileMap.GetTile(clickPos);

Then it doesn’t give me a warning but I cant get the custom variables i need.

If you know how I can fix this line or if you know a better method I could be doing this then I would greatly appreciate it. Thanks.

The error is telling you that you are missing a cast. Do the cast.

AStarTile selectedTile = (AStarTile) tileMap.GetTile(clickPos);

You probably want to do it more safely.

var tile = tileMap.GetTile(clickPos);
if(tile is AStarTile) {
   var selectedTile = (AStarTile) tile;
   // more code
}