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

Unity Tilemap and script

Discussion in 'Scripting' started by denis-andreevich, Nov 19, 2019.

  1. denis-andreevich

    denis-andreevich

    Joined:
    Aug 21, 2016
    Posts:
    50
    I have a Tilemap. How can I make it so that when a player approaches one of these objects, it triggers its own specific task, which is randomly selected from the List?
    If I did so for each particular game object, then inside the script implemented Random.Range. In the case of Tilemap, I don't get it that way, as one task extends to every all objects inside it.
     
  2. Emolk

    Emolk

    Joined:
    Feb 11, 2014
    Posts:
    241
    It's a little bit confusing what you are asking here, could you extrapolate?

    If i'm reading your question correctly, you could in your update method track the closest tile to the Player. When the Player falls within distance of the tile, activate something.

    You can't have individual scripts for each tile, that's one of the reasons tiles are so cheap performance wise.
     
  3. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi @denis-andreevich,

    You could create a data structure that in a way mirrors the tilemap, maybe use coordinates as reference and then store information to that data structure in a format that is good for you. Or use a hybrid approach by having the "dummy" tilemap for visuals (and maybe collisions if you use tilemap colliders), then add objects on the tilemap grid, define the needed functionality to those GameObjects and use triggers/colliders or some other way to interact with them. I've used that and it worked quite ok for me.

    You might find this thread useful:
    https://forum.unity.com/threads/tiles-customization-additional-data-and-functions-tilemaps.511438/
     
  4. denis-andreevich

    denis-andreevich

    Joined:
    Aug 21, 2016
    Posts:
    50