Search Unity

How to delete a Tile on Collision ?

Discussion in '2D' started by omeith, Jul 16, 2018.

  1. omeith

    omeith

    Joined:
    Jul 16, 2018
    Posts:
    3
    Hello Guys ,

    I's like to implement a logic , that destroys a Tile , if the player hits it .
    I don't get the point , how I "connect" my Tilemap to the player Class .
    My Tilemap is not having its own Script . What is best practise ?
    Hope you can help me !


    Thank You !
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    rileyedmunds7 likes this.
  3. omeith

    omeith

    Joined:
    Jul 16, 2018
    Posts:
    3
    Thank you for awnsering .
    I have already check out this solution but dont understand it, and propably this is why its not working .

    He declares the tilemap attribute and later assigns a Tilemap to it , with GetComponent... .
    But why is Tilemap a Component of the GameObject ?

    I dont get the point , how its getting my createt Tilemap ?


    Code (CSharp):
    1. public GameObject tilemapGameObject;
    2. Tilemap tilemap;
    3. tilemap = tilemapGameObject.GetComponent<Tilemap>();

    Sorry for not perfect English .
     
  4. omeith

    omeith

    Joined:
    Jul 16, 2018
    Posts:
    3
    So now I know what the problem is ...
    My Tilemap is always null .
     
  5. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    I assume they assign the GameObject "tilemapGameObject" in the inspector. That object contains the component Tilemap.

    They could have made the Tilemap type variable public, and assign it that way, but they decided to make it a GameObject and use GetComponent.

    If you only have one tilemap in the scene, you can try using this on Awake or Start:

    tilemap = FindObjectOfType<Tilemap>();


    Please note that this function is expensive, and it's best not to use it if you can just assign it in the inspector.
     
  6. rileyedmunds7

    rileyedmunds7

    Joined:
    Sep 3, 2021
    Posts:
    1
  7. Durium

    Durium

    Joined:
    Feb 1, 2020
    Posts:
    37
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    See my response to your duplicate post here.