how do i delete a trigger when it is entered?
I have bullets in my game that are triggers and I want it so that when they collide with an object (preferably a tag) they delete themselves. However when I look for this question I find the opposite. I am not looking to delete what it touches, I want it to delete itself when entered. Thank you...
void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "Floor")
{
Destroy(gameObject);
}
}
Answer by TBruce · Apr 06, 2016 at 04:47 AM
@Buzzsaw5000 You should use a manager for destroying unwanted objects and not in the class attached to the GameObject itself. This would be a lets say a part of your game manager or even a separate script attached to another object. You might want to do something like the following:
First create a class something like this:
public class ObjectManager : MonoBehaviour {
public void RemoveObject(GameObject go, float delay)
{
if (go != null)
{
go.SetActive(false);
StartCoroutine(DestroyGameObject(go, delay));
}
}
IEnumerator DestroyGameObject(GameObject go, float delay)
{
yield return new WaitForSeconds(delay);
Destroy(go);
}
}
Next inside OnCollisionEnter call objectManager.RemoveObject(bullet gameobject, delay).
You could take this even farther by creating a queue to destroy everything.
Thank you. However it was a bit too late, but I found the answer by making a script that deletes bullets when contacting a specific object, but this also work so thank you. Here is what I did, I just have to remember to put it on the floor and walls.
public class DestroyBullet : $$anonymous$$onoBehaviour
{
public bool DestroyAllBullets = false;
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "PlayerBullet")
{
Destroy(col.gameObject);
}
if (col.gameObject.tag == "EnemyBullet")
{
Destroy(col.gameObject);
}
if (col.gameObject.tag == "EnemyFastBullet" && DestroyAllBullets == true)
{
Destroy(col.gameObject);
}
}
}
@$$anonymous$$avina Sorry if I wasted you time but you did help and hopefully it helps someone else.
I am glad that you found a solution to your problem.
Your answer
Follow this Question
Related Questions
I can't figure out why a game object isn't being destroyed. 0 Answers
Need help coding collision detection to a destroy command. 0 Answers
2D: Destroy object with dynamic collider after exiting object with static collider 1 Answer
How to Destroy A GameObject when it enters any Triggers 2 Answers
Unity 2D sorting item by color 0 Answers