Question by
OneToMany · Apr 13, 2020 at 01:10 PM ·
2d-platformertilemaptile
How can I Remove one tile from tilemap
Hi, I want to remove one tile from tilemaps and I wrote this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class MouseClick : MonoBehaviour
{
public Grid grid; // You can also use the Tilemap object , note that Grid And Tilemap are same positions
public Tilemap[] tilemap;
void Start()
{
grid = GameObject.Find(nameof(Grid)).GetComponent<Grid>();
tilemap = GameObject.FindObjectsOfType<Tilemap>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3Int coordinate = grid.WorldToCell(mouseWorldPos);
print($"Grid coordinate: {coordinate}");
foreach (var item in tilemap)
{
RemoveTile(item, coordinate);
print("REMOVED!!!!!!!!!!!");
}
}
}
private void RemoveTile(Tilemap tileMap, Vector3Int position)
{
tileMap.SetTile(position,null);
}
}
but not work. can anybody help me?
Comment
Best Answer
Answer by OneToMany · Apr 13, 2020 at 01:24 PM
I found answer, just set Z to zero:
coordinate.z = 0;
and now work fine!
Your answer
Follow this Question
Related Questions
How to get custom data from a tile 0 Answers
How can I change the alpha of a Tile Sprite 2 Answers
What is that and why is it happening? 0 Answers