- Home /
Cant delete clones of prefab
I cant delete the clones of a prefab i have. i have tried lots of things. i have a 2d box collider which i have applied this script to(istrigger is set to true). and another script which spawns clones of aprefab. I have tried a lot of stuff and i cant get it to destroy the prefab clones or anything at all. any help appreciated
using System.Collections; using System.Collections.Generic; using UnityEngine; public class BoxCookieDeleterC : MonoBehaviour { public void OnTriggerEnter(Collider other) { if (other.gameObject.tag.Equals("cookies")) { Destroy(other.gameObject); } } }
,I have a 2d box collider which i applied this code to: `
I don't see a problem with your code... Could you show the instantiation part?
If you make a 2D game though, be careful to use OnTriggerEnter2D ins$$anonymous$$d of OnTriggerEnter.
Answer by Kristifor_p · Sep 15, 2019 at 09:50 AM
Here is what i got for you, this is my variation for removing prefab clones.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BoxCookieDestroy : MonoBehaviour
{
public GameObject yourPrefab; // Select your prefab that you are cloning.
public void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "cookies")
{
Destroy(yourPrefab); // This will destroy your prefab if it get triggered with the box collider.
}
}
}
Let me know if this help you out :) @unity_aThFfWJj_Iimrw
Your answer
Follow this Question
Related Questions
How to (in code) save a gameobject that already exists in my scene as a prefab? (c#) 0 Answers
Instantiated clones arent behaving the same as the prefab 1 Answer
Call a function in an instanciated prefab has no effect on the prefab 2 Answers
Insert a prefab into scene in a script? 1 Answer
How to tell at runtime if a GameObject is a prefab 3 Answers