2
\$\begingroup\$

I was trying to implement a destructable tile like:

public class SpikeTile : Tile
{
  public int health = 3;
  public void TakeDamage(int dmg)
  {
    Debug.Log("Tile takes damage: " + dmg + ", health: " + health);
    health -= dmg;
}

And I will be able to remove it by using:

Vector3Int cell = tilemap.WorldToCell(collision_pos);

tilemap.SetTile(cell, null);

But it doesn't create a health value per tile! It is shared.

So I've also tried attach a GameObject with DestructibleTile to the above scriptable tile and:

DestructibleTile : MonoBehavior 
{
    public int health = 3;

    // same logic
}

But it's also shared between all tiles! The stat is not per tile. (And after I attach the GameObject to customized Tile, it's impossible to SetTile to null anymore.)

Even tried create an array for all tiles to store the stats:

DestructibleTile : MonoBehavior 
{
    public int[] health;

    // no idea how to map this to each tile
}
\$\endgroup\$
  • \$\begingroup\$ Well, a post is about this, but it doesn't make use of vanilla tile utilities. It created a new dictionary to store the tile stats. idk if this is a good solution but it seems to work. \$\endgroup\$ – knh190 Feb 13 '19 at 4:49
3
\$\begingroup\$

I remember Unity guys explaining that each tile of the same type is not a separate instance. Instead the tile system visualizes literary the same tile multiple times. This is a necessary limitation to achieve decent tilemap performance.

There is a GitHub repo at Unity's profile called 2d-extras. It contains a system called GridInformation that allows assigning properties per grid position. You can find it at: https://github.com/Unity-Technologies/2d-extras

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Thanks for sharing! I've already been using it - instead of using GridInformation, Prefab Brush solved my problem. \$\endgroup\$ – knh190 Feb 13 '19 at 15:07
  • \$\begingroup\$ Prefab brush was the other thing I thought about. But depending on the size of the tilemap and what you attach to the prefab game object it may later become performance problem if your levels are huge. But otherwise it's a solution too. \$\endgroup\$ – Nikaas Feb 14 '19 at 7:48
  • \$\begingroup\$ GridInformation is the same implementation I linked in my comment (see question below). But, yeah, I agree with you. Prefab brush is a quick solution though. \$\endgroup\$ – knh190 Feb 15 '19 at 2:31
2
\$\begingroup\$

For anyone coming later for this question:

Can I use customized Tile to store stats per tile? The answer is, No. If you define:

public class MyTile : Tile
{
  public float health = 3;
}

The value is shared among all tiles. It's design for the performance purpose - all tiles will share same attributes and render it multiple times.

The solution I used is creating GameObject. Yeah, making use of Prefab Brush defined in 2d-extra. Following the steps:

  1. Create a prefab brush
  2. Attach your prefab to the dedicated brush created in 1.
  3. Use this prefab brush to create gameObjects
  4. Interact with the gameObjects then
|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.