- Home /
Scriptable tile
Trying to follow some tutorials and create a scriptable tile for the Tilemap system, I created a script with some public variables, when I use GetTile() I manage to get a reference to the scriptable tile, but I'm unable to access those public variable, why can't I access the three public variables in this script?
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UnityEngine.Tilemaps
{
public enum GroundType { Grass, GrassWet, Metal, Wood, Sand}
public class GroundTile : Tile
{
public Sprite tileSprite;
public GroundType groundType;
public int layer;
public override void GetTileData(Vector3Int location, ITilemap tileMap, ref TileData tileData)
{
base.GetTileData(location, tileMap, ref tileData);
{
tileData.sprite = tileSprite;
tileData.colliderType = Tile.ColliderType.Sprite;
}
}
#if UNITY_EDITOR
[MenuItem("Assets/Create/Ground Tile")]
public static void CreateGroundTile()
{
string path = EditorUtility.SaveFilePanelInProject("Save Ground Tile", "New Ground Tile", "asset", "Save Ground Tile", "Assets");
if (path == "")
return;
AssetDatabase.CreateAsset(ScriptableObject.CreateInstance<GroundTile>(), path);
}
#endif
}
#if UNITY_EDITOR
[CustomEditor(typeof(GroundTile))]
public class GroundTileEditor : Editor
{
public GroundTile tile { get { return (target as GroundTile); } }
public override void OnInspectorGUI()
{
EditorGUI.BeginChangeCheck();
tile.tileSprite = (Sprite)EditorGUILayout.ObjectField("Sprite", tile.tileSprite, typeof(Sprite), false);
tile.groundType = (GroundType)EditorGUILayout.EnumPopup("Ground Type", tile.groundType);
tile.layer = EditorGUILayout.LayerField("Layer", tile.layer);
if (EditorGUI.EndChangeCheck())
EditorUtility.SetDirty(tile);
}
#endif
}
This prints: MyTile (UnityEngine.Tilemaps.GroundTile)
Tilemap tileMap = collider.GetComponent<Tilemap>();
TileBase currentTile = tileMap.GetTile(tileMap.WorldToCell(collisionPos));
print(currentTile);
Now that I have a reference to my custom tile, how do I get those public variables?
Answer by cryingwolf85 · Aug 03, 2018 at 04:20 PM
Instead of TileBase currentTile = tileMap.GetTile(tileMap.WorldToCell(collisionPos));
Try this: GroundTile currentTile = tileMap.GetTile(tileMap.WorldToCell(collisionPos)) as GroundTile;
If you have more than one type of tile, and you're not sure of the tile type at the time, you can do something like this:
TileBase currentTile = tileMap.GetTile(tileMap.WorldToCell(collisionPos));
if (currentTile is GroundTile) {
// Do stuff for a ground tile
}
else if (currentTile is WaterTile) {
// Do stuff for a water tile
}
// .. etc
Thank you so much, tried that earlier but was missing "as GroundTile", it works!
You sir are the $$anonymous$$VP! Was banging my head for hours against this
Your answer
Follow this Question
Related Questions
Need tiles to contain data or a reference 0 Answers
How can I modify the sprite of a particular Scriptable Tile in a Tilemap? 2 Answers
public variable not updating (I use HideInspector) 1 Answer
Unity 2D TileMap Rule Tile Carpet 1 Answer
Bake Lights Seamlessly onto 3D Connected Areas/Tiles? 0 Answers