- Home /
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.
Answer by ThisIsDangerous · Feb 25, 2020 at 04:45 AM
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 }
Thank you. That worked perfectly. Could you help me with one more thing please? Im trying to change the tiles sprite from the Astar script. I have added this function in the AStarTile script public void ChangeTile(Vector3Int position, ITilemap tilemap, ref TileData tileData, Sprite newSprite) { tileData.sprite = newSprite; tilemap.RefreshTile(position); }
Then I am calling the function from my Astar script with this line: selectedTile.ChangeTile(clickPos, tile$$anonymous$$ap, , selectedTile.litSide);
How do I get the "ref TileData tileData"? I dont understand what to put in the 3rd slot for ChangeTile. Thanks
Actually I haven't had a chance to work with scriptable tiles and don't know how exactly it should be done. But since you want to change the rendered tile sprite, I'd just try and call selectedTile.sprite = yourNewSprite; probably this will work as you intend.
So in your example it should look something like this:
if (selectedTile.currentlyLit == false) { selectedTile.currentlyLit = true; selectedTile.sprite(selectedTile.litSide); }
It doesn't let me do that unfortunately. I get the error "Non-invocable member 'Tile.sprite' cannot be used like a method". But thanks for trying, and for answering my initial question.