Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to find the GameObject attached to a Tile?

Discussion in '2D' started by Roldo, May 5, 2020.

  1. Roldo

    Roldo

    Joined:
    Oct 25, 2014
    Posts:
    41
    Hello

    I am currently trying to figure out how to find the GameObject that is attacjed to a tile on a tilemap. The GetTile method returns a TileBase object, and it does not have a gameObject attached to it(unlike the Tile object). However, i can not simply cast the TileBase to a Tile, as i am using tile's from the 2d extensions package, specifically RuleTile. I have an idea of maybe getting the tile's world position and finding a GameObject at that position, however will be very inefficient and will potentially lead to bugs.

    Is there any way to find a GameObject that is attached to the tile?
     
  2. Dedi6

    Dedi6

    Joined:
    Jan 20, 2019
    Posts:
    119
    Code (CSharp):
    1. RuleTile currentRuleTile = currentTileMap.GetTile(currentTileMap.WorldToCell(*whateverPositionUWant*.position)) as RuleTile;
    Have you tried getting the GetTile as RuleTile?
     
  3. Roldo

    Roldo

    Joined:
    Oct 25, 2014
    Posts:
    41
    Yes, i have, however as far as i can see, RuleTile does not store a reference to it's game object instance, only to the prefab it instantiates
     
  4. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068