- Home /
problem with destroying objects
Hello so im using the GameObject.FindGameObjectsWithTag and whenever i run the game it wont destroy my prefabs when i try to destroy them, And i even applied the tag on the gameobject whats going On?
function Erase() {
if (HitBlock())
Destroy(hit.transform.GameObject.FindGameObjectsWithTag("Block"));
Answer by syclamoth · Dec 22, 2011 at 03:06 AM
Yeah, that's not how you use Destroy. This will destroy the array returned by FindWithTag, not the actual objects contained inside it!
Also, you are using
hit.transform.GameObject
there is no such member in transform. If you are trying to find only those gameObjects that are children of that transform, you need to check the individual members returned by the class function FindGameObjectsWithTag.
What you should be using instead is
for(var obj : GameObject in GameObject.FindGameObjectsWithTag("Block"))
{
if(obj.transform.IsChildOf(hit.transform))
{
Destroy(obj);
}
}
Of course, this assumes that hit is a valid variable, and has a member called 'transform' that is a Transform. Make sure you're doing all of that, too.
Your answer
Follow this Question
Related Questions
Help me to manage game memory 1 Answer
Spawn Random Objects on a Sphere Surface 0 Answers
Procedural platformer: add/remove or pooling for platforms? 2 Answers
Instantiate Pre-fab clone altered on Instantiate 2 Answers
How to destroy an object? 2 Answers