- Home /
How can I find the name of the tile that I collided with in 2d?
I'm trying to make a game where you can farm metals. I'm just using the standard tile system where I have 20 tiles. One is grass, the others are metal plants and the seeds for them. Say I want it where if I collide with a bronze plant, I can do something, such as Debug.Log("Bronze") for starters? This is all I have for now, it is currently doing nothing when I hit the bronze plant, so not very useful.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FindPlant : MonoBehaviour
{
void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "tilemap_1")
{
Debug.Log("Bronze");
}
}
}
What exactly do you want to do? It seems like you already know what to do
Right now nothing happens. I've tried na$$anonymous$$g the tile bronze, I've fidgeted with adding colliders, triggers, etc, but it hasn't worked. Basically I am clueless as to what to try, what do you think I could be doing wrong or should try?
before checking if its colliding with te tilemap_1 add a log outside the if. to collision to happen both need colliders (both 2d or both 3d) and atleast one rigidbody. also none of the gameobjects can have the istrigger ticked
Answer by Crakker1337 · Nov 18, 2020 at 09:10 PM
You must have at least one rigidbody attached in order to OnCollisionEnter work. (Source)
Your answer
Follow this Question
Related Questions
Player ignores tilemap collider on fall 1 Answer
2D world generation script generating strange world made up of large lines 0 Answers
2D Tilemap increase custom Brush size 0 Answers
Get a tile relative to its position in the world 1 Answer
Box collider 2d detecting collisions while not actually colliding 1 Answer