- Home /
Why can't I destroy this object!?
I'm trying to Destroy my object but it won't work!! Please Help! ;-;
Here is the code:
using System.Collections;
using UnityEngine;
using System.Collections;
public class Strawberries : MonoBehaviour {
public int score = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D other){
Debug.Log ("Triggered");
Destroy (gameObject);
}
}
[1]: /storage/temp/101306-screen-shot-2017-09-05-at-72908-pm.png
Answer by $$anonymous$$ · Sep 05, 2017 at 10:39 AM
Does The Debug.Log("Triggered") Work? If Not, Then The OnTriggerEnter2D Doesn't Get Called Because Of 2 Reasons:
If The "Is Trigger" Isn't Set To True On The Collider That Has The "Strawberries" Script.
If The Other Object That Enters This Collider Hasn't A Rigidbody2D Attached To It.
So You Need To Have A "Rigidbody2D" Attached To The GameObject That Enters The Collider Of This GameObject That Holds "Strawberries" Script.
Hope It Helps.
The other object does have a Rigidbody attached to it and I did set the "Is trigger" on true but it still won't work for some reason.
Can you check Physics2D settings, are these two objects on layers that don't collide?
Answer by irfan-ayub · Sep 06, 2017 at 06:25 AM
try this.
void OnTriggerEnter2D(Collider2D other){
Debug.Log ("Triggered");
Destroy (this.gameObject);
}
or you can try to attach the script on other object (your player in colliding with) and then do what @tmalhassan said.
and if this Debug.Log ("Triggered"); is not printing anything on Console then you might have another error can you post a screenshot of your console or you can cross check if the gameobject is not being effected by any other object or reference to any other script and forcing it not to destroy for some reason.
Answer by hihihihihihihihihi · Feb 10, 2019 at 05:04 AM
You need to check whether you are destroying the gameObject with the Collider... If it is the parent or child object you want to destroy...