How can I get the TileData needed to use the TileBase.GetTileData function?

I have created the following custom tile and slightly altered the “GetTileData” function, to become my “ChangeTile” function. It requires the same information to be passed over, but also requires a Sprite:

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

    public void ChangeTile(Vector3Int position, ITilemap tilemap, ref TileData tileData, Sprite newSprite)
    {
        tileData.sprite = newSprite;
        tilemap.RefreshTile(position);
    }
}

I am using the following line to call the function:

selectedTile.ChangeTile(clickPos, tileMap, ,  selectedTile.litSide);

I dont understand how I find “ref TileData tileData” needed for the function. Like what is the exact thing I put in the 3rd slot of the line above?
I can provide the full script if you need more information. Thanks.

Almost a year late, but if you take a look at the TileBase documentation (from which Tile is inherited), you’ll find several methods to override called GetTileData and RefreshTile. Within these methods (gleaning usage details from the examples on each page), your code has access to all the information required to perform a change to what sprite is displayed. Of course, your code will have to provide what sprites are used and how to determine which is shown, but the examples are quite informative!