C
C#2y ago
Raptorini

✅ How to reference gameObject in OnCollisionEnter2D?

public void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "wall_up")
{
Debug.Log("Collision detected!");

}
}
public void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "wall_up")
{
Debug.Log("Collision detected!");

}
}
I don't want to detect collision of the game object the script is tied to, I want to detect collision of an instantiated object called _cell. How do I reference _cell in the collision function?
7 Replies
Anchy
Anchy2y ago
col.gameObject
col.gameObject
Raptorini
RaptoriniOP2y ago
Do i replace the col or gameObject with _cell, or add it somewhere?
Anchy
Anchy2y ago
detect the collider you are colliding with by checking the tag name then if your condition is true you can safely assume col.gameObject is your cell object what tag does your cell have?
Raptorini
RaptoriniOP2y ago
cellTag
Anchy
Anchy2y ago
check for that tag then
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Raptorini
RaptoriniOP2y ago
Ah yeah
Want results from more Discord servers?
Add your server