Search Unity

Is there any way that I could differentiate between two kinds of tiles?

Discussion in '2D' started by fusenhanj5_unity, Nov 2, 2018.

  1. fusenhanj5_unity

    fusenhanj5_unity

    Joined:
    Nov 2, 2018
    Posts:
    1
    Hi
    If I want to add both breakable tile and unbreakable tile.

    Is there any way that I could differentiate between two kinds of tiles?

    like using Tag
    Code (CSharp):
    1. if (tag == "breakableObject")
    2. {
    3.     Destroy(gameObject);
    4. }
    I did find this (start from 19:15), add a script to one kind of tile, and I also add below to destroy a tile.
    (It works for normal tilemap with MonoBehaviour script)

    BreakableTile.cs
    Code (CSharp):
    1.     void Update ()
    2.     {
    3.         if (Input.GetMouseButtonUp(0))
    4.         {
    5.             Vector3 pz = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    6.             pz.z = 0;
    7.  
    8.             GridLayout gridLayout = transform.parent.GetComponentInParent<GridLayout>();
    9.             Vector3Int cellPosition = gridLayout.WorldToCell(pz);
    10.  
    11.             player.transform.position = cellPosition + Vector3.Scale(new Vector3(0.3f, 0.6f, 0f), gridLayout.cellSize);
    12.             tilemap.SetTile(cellPosition, null);
    13.     }
    But it seems doesn't work, could someone please help me?


    ----------------------------------------------------------------------------------------------------------------
    I finally figured out how to solve this problem

    Code (CSharp):
    1. if (tilemap.GetTile(position).GetType().ToString() == ("GameObject name")) { }
    or
    Code (CSharp):
    1. if (tilemap.GetTile(position).name.Equals("Sprite name")) { }
    both work for me.
     
    Last edited: Nov 3, 2018
    tjPark likes this.
  2. tjPark

    tjPark

    Joined:
    Mar 19, 2015
    Posts:
    13
    If they just give the one simple value field of any(int, string whatever), it would be much easier for adjusting value in inspector together