Search Unity

How do I remove tiles at mouse pos?

Discussion in '2D' started by AaronKanaron, Nov 29, 2020.

  1. AaronKanaron

    AaronKanaron

    Joined:
    Aug 11, 2020
    Posts:
    13
    Hello! I am trying to do a system where you can remove tiles with a left-click.
    Currently this is my code
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Tilemaps;
    3.  
    4. public class TileSpawner : MonoBehaviour
    5. {
    6.     public Tile GroundTile;
    7.     public Tilemap tilemp;
    8.  
    9.     void Update()
    10.     {
    11.        
    12.         Vector3 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    13.         if (Input.GetMouseButtonDown(0)) {
    14.             Vector3Int selectedTile = tilemp.WorldToCell(point);
    15.             tilemp.SetTile(selectedTile, null);
    16.         }
    17.     }
    18. }
    19.  
    It gives me errors at the
    Vector3 point = Camera.main.ScreenToWorldPoint(Input.mousePosition)
    "Object reference not set to an instance of an object"
    Does anyone know how to fix this problem or is there an easier way to remove tiles at mouse position? Thanks in advance!
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Do you have a Camera with tag in scene?

    And have you assigned a Tilemap to your "tilemp" field?
     
    AaronKanaron likes this.
  3. AaronKanaron

    AaronKanaron

    Joined:
    Aug 11, 2020
    Posts:
    13
    Thanks! This worked, but now I'm getting this error instead at line 14

    Vector3Int selectedTile = tilemp.WorldToCell(point);

    Object reference not set to an instance of an object
     
  4. AaronKanaron

    AaronKanaron

    Joined:
    Aug 11, 2020
    Posts:
    13
    Okay sorry, I forgot to assign the tilemap, but it won't destroy on the mouse pos.
    This video can explain what I mean, this is my code now. And it doesn't give me any errors.



    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Tilemaps;
    3.  
    4. public class TileSpawner : MonoBehaviour
    5. {
    6.     public Tile GroundTile;
    7.     public Tilemap tilemapLayer1;
    8.  
    9.     void Update()
    10.     {
    11.         Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    12.        
    13.         if (Input.GetMouseButtonDown(0))
    14.         {
    15.             Vector3Int selectedTile = tilemapLayer1.WorldToCell(pos);
    16.             tilemapLayer1.SetTile(selectedTile, null);
    17.         }
    18.     }
    19. }
    20.  
     
  5. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
  6. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Another thing might be your camera.

    Seems like in your video, tile always gets removed near the center of screen.

    So are you using Perspective or Orthogonal camera?

    If you are using Perspective camera, ScreenToWorldPoint won't work like that.

    But with Orthogonal camera it should work.
     
    lilacsky824 likes this.
  7. AaronKanaron

    AaronKanaron

    Joined:
    Aug 11, 2020
    Posts:
    13
    It worked when disabling the player. But there is nothing referencing a tilemap in the player script, or any script at all except this one.
     
  8. AaronKanaron

    AaronKanaron

    Joined:
    Aug 11, 2020
    Posts:
    13
    Thanks so much! The perspective to orthogonal thing worked!
     
    eses likes this.
  9. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    NP.

    And if you ever need to use perspective camera, look into raycasting colliders. You can convert screen point to ray.

    And why not drop a like for my posts? ;)
     
    ChuanXin likes this.