- Home /
Destroy 2D Objects when in contact
hi, i'm trying to do some exercising by making infinite runner game and currently i'm having trouble when destroying generated obstacle. The problem is that it doesn't destroy the obstacles at all when the obstacle come in contact with the destroyer
This is the script used to destroy obstacles which i obtained from this forum but it still doesn't work
using UnityEngine;
using System.Collections;
public class DestroyObstacle : MonoBehaviour
{
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Obstacle")
{
Destroy(col.gameObject);
}
}
}
and i also have tagged all the obstacles as "Obstacle" and keep the Is Trigger box unchecked i'll provide the details in the attachment
The prefab named "rock" is a child to a parent prefab called "Obstacle[n]" also tagged Obstacle
i've tried searching all over the place but still can't find the solution, i'm sure i missed something
Thanks in Advance!
it works, i added rigidbody to both obstacles and destroyer wall, now it can destroy the obstacle like it should but new problem occurs, since the obstacle need to be frozen the rigidbody2d messes it up now the obstacles twitching like crazy.
and why do you have to enable the rigidbody when it's not enemy or player that affected by physics?
In the Rigidbody2D settings, give Gravity scale 0 and under Constraints you can freeze positions. You have to have a Rigidbody for Colliding statements
One of them should have the rigidbody. This script should be attached to the obstacle.