Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to replace a TileMap Gameobject?

Discussion in 'Scripting' started by Nightland_Driver, Feb 1, 2022.

  1. Nightland_Driver

    Nightland_Driver

    Joined:
    Nov 21, 2021
    Posts:
    13
    Hey,

    I’m currently working on a game with a GameObject tilemap (It's a 3D Game).
    Now I want to use a script to replace the GameObject which was selected with a Mouseclick.
    Unfortunately, I couldn't found anything about how to change the GameObject.

    Can somebody help me with that?

    This is my code to Check where the Player has clicked:
    Code (CSharp):
    1. public Tilemap tilemap;
    2.  
    3. void Update() {
    4.     if (Input.GetMouseButtonDown(0)) {
    5.         RaycastHit hit;
    6.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    7.         if (Physics.Raycast(ray, out hit)) {
    8.             //Replace Gameobject in Tilemap
    9.         }
    10.     }
    11. }
     
  2. Cameron_SM

    Cameron_SM

    Joined:
    Jun 1, 2009
    Posts:
    915
  3. Nightland_Driver

    Nightland_Driver

    Joined:
    Nov 21, 2021
    Posts:
    13
    Hey

    Thank you for your answer.
    I have now removed the ray casting and added the grid coordinates.
    Unfortunately, I can’t find how to change the GameObject. For 2D tilemaps I found it, but the function needs a tile and not a GameObject.


    Code (CSharp):
    1. public Tilemap tilemap;
    2. public GameObject newObject;
    3.  
    4. void Update() {
    5.     if (Input.GetMouseButtonDown(0)) {
    6.         Vector3 position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    7.         tilemap.SetTile(tilemap.WorldToCell(position), newObject);
    8.     }
    9. }
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,622
    Maybe I ask what have 2D Tilemaps have to do with the GameObjects here? Tilemaps are about tiles, not GameObjects. Are these the Tilemap Brush?

    Is there a tutorial you're following or some docs somewhere?
     
  5. Nightland_Driver

    Nightland_Driver

    Joined:
    Nov 21, 2021
    Posts:
    13
    No, I don’t follow a tutorial, I just used the two docs that were posted here.
    Let me explain what I’m trying to do.

    I have a tilemap with GameObjects. Like in this Video.

    Now I want to click on a tile to replace this with another tile.

    For example: I click on a Grass Tile and it changes this Tile to a Path tile.
    I’d like to know how to do that.

    If I understand the docs correctly, I can convert the position where I clicked in the world with "WorldToCell(position)" into the Tile Coordinate and then change the Tile.
     
    Last edited: Feb 2, 2022
  6. Nightland_Driver

    Nightland_Driver

    Joined:
    Nov 21, 2021
    Posts:
    13
    Update:

    Code (CSharp):
    1. public Tilemap tilemap;
    2. public GridLayout gridLayout;
    3.  
    4. public GameObject newObject;
    5.  
    6. void Start() {
    7.     Tilemap tilemap = GetComponent<Tilemap>();
    8. }
    9.  
    10. void Update() {
    11.     if (Input.GetMouseButtonDown(0)) {
    12.         Plane plane = new Plane(Vector3.up, 0);
    13.  
    14.         float distance;
    15.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    16.  
    17.         if (plane.Raycast(ray, out distance)) {
    18.             Vector3 worldPosition = ray.GetPoint(distance);
    19.             Debug.Log("World Position: " + worldPosition);
    20.  
    21.             Vector3 gridCell = gridLayout.WorldToCell(worldPosition);
    22.             Debug.Log("Grid Cell: " + gridCell);
    23.  
    24.             Vector3Int gridCellInt = Vector3Int.FloorToInt(gridCell);
    25.             Debug.Log("Output: " + tilemap.GetTile(gridCellInt));
    26.         }
    27.     }
    28. }
    I have edited the code and now it returns the exact gridCell of the tile
    Example: (1.00, -6.00, 0.00)

    Unfortunately I couldn’t find anything to replace a GameObject tiles.
    Does anyone know how to do that or if it’s even possible?
     
  7. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    The GameObject Brush shown in the video does not actually add or make use of Tiles or the Tilemap. It uses the Grid for calculating the position where a GameObject should be placed or erased.

    You may want to make use of Raycast with the RaycastHit parameter, which will give you more details on the GameObject you have hit, for example:

    Code (CSharp):
    1.  
    2. public Tilemap tilemap;
    3. public GridLayout gridLayout;
    4. public GameObject newObject;
    5. void Start() {
    6.     Tilemap tilemap = GetComponent<Tilemap>();
    7. }
    8. void Update() {
    9.     if (Input.GetMouseButtonDown(0)) {
    10.         Plane plane = new Plane(Vector3.up, 0);
    11.         RaycastHit hitInfo;
    12.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    13.         if (plane.Raycast(ray, out hitInfo)) {
    14.             // Destroy GameObject hit
    15.             Destroy(hitInfo.collider.gameObject);
    16.             // Replace GameObject
    17.             Vector3 worldPosition = ray.GetPoint(distance);
    18.             Vector3 gridCell = gridLayout.WorldToCell(worldPosition);
    19.             Vector3Int gridCellInt = Vector3Int.FloorToInt(gridCell);
    20.             var newGameObject = new GameObject(); // Replace this with your actual GameObject instance
    21.             newGameObject.transform.position = gridLayout.CellToWorld(gridCellInt);
    22.         }
    23.     }
    24. }
    If I am wrong and you are using Tiles, you can use the Tilemap.SetTile function to replace the Tile at the position.

    Hope this helps!