2D tilemap item collision deletes all items?
Hello! So, when my player collides with the item (coins) it deletes all instances of the coins in the scene. The coin is a tilemap and I've just attached a tilemap collider 2D and a script to the inspector of the coin tilemap with the Is Trigger box checked.
I just want one coin to be deleted at a time when the player collides with it instead of all coins at the same time, but I have no idea how to go about doing so...
Here is the coin script I have attached to the tilemap:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Coin : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.tag == "Player")
{
CoinCounter.coinAmt += 1;
Destroy(gameObject);
}
}
}
Answer by streeetwalker · Jun 29, 2020 at 06:03 AM
@unity_WAsXmOs-rROT3Q The problem seems to be with what you are doing with the Tilemap concept - why did you make a coin a Tilemap? You need to provide more information, and some screenshots of your set up, because it doesn't make senese.
Tilemaps are objects that contain Tiles. Tiles contain Sprites, which give Tiles a graphical representation in the Tilemap
If your coins are Tiles in a Tilemap, then you do not want to Destroy the Tilemap that contains them. If you want to make it look like the game character is picking up coins, you would replace the coin Tile sprite with another sprite that did not contain the coin image, or you could replace the entire Tile.
Your answer
Follow this Question
Related Questions
Floating Sprite Unity 2018.1.2 1 Answer
Collision doesnt get removed when I erase parts of the tilemap 0 Answers
How do I make an object with a rigidbody2D and boxcollider2D not collide with another? 1 Answer
How to destroy a tile on collision in several parts 0 Answers
a chain of 2d collision problem ( object going trough other ) 0 Answers