Problem adding asset of type Tile to object that is derived from type Tile...

I searched quite a bit and couldn’t find a solution, so hopefully there is a solution to the problem. Basically, I have the following, very simple class:

using UnityEngine;
using UnityEngine.Tilemaps;

public class GameTile : Tile
{
    public bool locked = false;
}

This class defines the additional information I would like to retain as I build my Tilemap programmatically. then, I serialize this and a few other fields as follows:

using System;

using UnityEngine;
using UnityEngine.Tilemaps;

[Serializable]
public class TileInfo
{
    public GameTile tile;
    public int width;
    public int height;
    public bool locked;
}

The program that pulls this all together, making it available to the editor does the following:

using System.Collections;
using System.Collections.Generic;

using UnityEngine;
using UnityEngine.Tilemaps;

public class DrawTilemap : MonoBehaviour
{
    [SerializeField]
    public TileInfo borderInfo;
    public TileInfo landInfo;

....

At this point, the editor looks good and has my fields available to populate:

6710395--771277--Screen Shot 2021-01-10 at 5.20.06 PM.png

When I click button to find the Tiles I want to add, nothing comes up. After searches the Internet, I found that I can type “t:Tile” to get a list of Tile type assets to show up in the project explorer; however, the editor will not allow me to drag those objects into the input for the GameTile type.

So, is there a way programmatically to trip the editor into thinking that GameTile type is Tile type? I thought since GameTile was derived from Tile that it would behave the same but that doesn’t appear to be the case…

You’re thinking of it in reverse.

You have a variable of type GameTile. Yes, this IS a Tile, but you cannot drag any old Tile into that slot. It has to be a GameTile. Otherwise when code tried to access your locked field, it just would not be there.

The other way around works: if you have a Tile field, you CAN drag a GameTile into it because we know that GameTile has all the stuff necessary to do business as its base object, a Tile.

I think the magic sauce you want is instead of creating Tile objects (which derive from ScriptableObjects), you want to create GameTile objects. Those should be draggable into any place that a Tile is wanted, like in your grid, in here, etc.

If you’ve already made a bazillion tiles and don’t want to redo them all, it probably wouldn’t be hard to make an editor script to do them all for you.

1 Like

Yes, I get what you’re saying. Since my tile map is already written, I guess I can just keep a dictionary of TileInfo corresponding to the grid position of each tile. Thanks

1 Like