Hello everyone. I am trying to change sprite of specific tile in tilemap. I came up with something like this. Code (CSharp): public Tilemap tilemap; public Sprite[] sprites; void Start () { } public void ChangeTileTexture(Vector3Int coord) { coord.y -= 1;//it's one tile off from y Tile tile = (Tile)tilemap.GetTile(coord); Debug.Log("TExture '" + tile.sprite.ToString() + "'"); tile.sprite = sprites[0]; Debug.Log("TExture 2'" + tile.sprite.ToString() + "'"); } Log says that texture was changed but it doesnt change anything in the game or scene view. Sometimes when i hit play it changes sprite of every tile, it somehow replaces sprite in Tile Palette. How can I make it work?
OK I figured it out. Code (CSharp): public Tilemap tilemap; public Sprite[] sprites_dig; // Use this for initialization void Start () { } public void ChangeTileTexture(Vector3Int coord) { Tile tile = ScriptableObject.CreateInstance<Tile>(); tile.sprite = sprites_dig[0]; coord.y -= 1;//it's one tile off from y tilemap.SetTile(coord, tile); Debug.Log("Texture '" + tile.sprite.ToString() + "'"); }
The above solution will work in runtime. If you have an existing Tile with the Sprite you want to change to, you can directly call tilemap.SetTile(coord, tile) without having to create a new Tile.