How can I get the world position of a Tile?

Is there a method to get the world position of a tile? I have randomly generating platforms, and I want the next platform to spawn somewhere after the last tile of the previous one. The platform’s length is also varied. Is there a way to the world position of a tile, in a tilemap? Thank you and have a good day.

Do you mean the position? or what do you mean exactly?

Yeah I mean the position. I wanted to get a tile, and make a transform’s position that tile’s world position.

Hi @Ecir_Leumas

It would make it a great deal easier for us to help with your question if you took the time to be a bit more specific with posing the problem. There are hundreds of ways to get the position of things in Unity and which way would be very dependant on what your problem actually is. Without that information it is very difficult to come with any intelligent reply to help.

That being said if you can get a reference to the tile in question, then every single object in Unity has a transform. Therefore you can simply say;

// do something to find a reference to the tile e.g.
GameObject tile = GameObject.Find("myTile");

Vector3 tilesPosition = tile.transform.position;

But using GameObject.Find is not a very efficient way to do this, so expanding your explanation of what the problem is, would greatly help the community, help you.

Instead of getting that tile’s position, you can store the last tiles placement position + the length of the platform to find the ending position. From there you can do your calculation as that would be the ending tile to the platform and ill write pseudo code to give a reference.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Tilemaps;

public class platformGeneratorController : Monobehavior
    private Vector2 lastPlatformPosition;
    public Tilemap myTilemap;
    public Tile platformTile;

    public void generatePlatform()
        //Random height from origin of world
        //If you want it from last tile's placed position, then add lastPlatformPosition.y to the random height section in SetTile();
        int randomHeight = Random.Range(-5, 6);
        //How far this platform will spawn from the last one
        int randomDistanceAway = Random.Range(0, 5);
        //The length of the platform
        int randomLength = Random.Range(2, 5);

        if (lastPlatformPosition != null) {
            for (int i = 0; i < randomLength; i++)
                //For loop increments the position.x and places a tile at every incrementation for the randomLength
                myTilemap.SetTile(new Vector3Int((int)(i + randomDistanceAway + lastPlatformPosition.x), randomHeight, 0), platformTile);
            //This is called if it is the first tile
            for (int i = 0; i < randomLength; i++)
                myTilemap.SetTile(new Vector3Int(i + randomDistanceAway, randomHeight, 0), platformTile);

        //This is the end tile position of the last platform placed
        lastPlatformPosition = new Vector2(randomDistanceAway + lastPlatformPosition.x + randomLength, randomHeight);

I hope this helps and good luck with your project!