DestroyImmediate Prefabs

So I am writing an editor script that will take a prefab and create an instance of it with particular settings. Easy enough. Before I create an instance of it, however, I want to delete any instance of it in the scene with the same name, whether it is an instance of the prefab or just a game object with the same name. Now the problem I am running into is it will easily destroy the game objects with the same name but it will not destroy a game object connected to a prefab, instead it will disconnect the game object from the prefab. If I run the code again it will then destroy the game object since it is not connected to a prefab anymore.

Here is my code:

    GameObject oldGO = GameObject.Find(;

    while(oldGO != null && != "")
        oldGO = GameObject.Find(;

    newGO = (GameObject)GameObject.Instantiate(replaceObject); // creates a new instance of the object

Step 1- clicked load xml, destorys game objects of name "test" and disconnects game objects of name "test" that are connected to a prefab

alt text

Step 2- clicked load xml again, destroys remaining game objects of name "test"

alt text

Step 3- now all game objects named test are deleted

alt text

Can someone tell me why and how to fix my code so it will destroy the game objects connected to a prefab and not just disconnect them.

UPDATE: 01/05/11

I am not looking for a new solution of how to do what I am doing, I am trying to get an answer of why DestroyImmediate() is not working how it should. So I did more debugging and looked further into my issue and found what the true problem is.

So I am trying to destroy all instances of an object with a particular name, even if they are connected to a prefab, and I will then create a single instance of it with new data that I pull in from an xml. The idea is I never want more than one of this object in the scene at any time. To go about this I go through a loop to search for every game object with the same name and try to destroy it. The problem that I am running into is any game object that is connected to a prefab will, appear, to not get destroyed but rather it will disconnect from the prefab.

When I looked further I found out that it is destroying the instance of the prefab but immediately will create a new instance of the prefab with all of the same data and name but this new instance is not connected to the original prefab. What I want to know is why this is happening? Why is it recreating the object after destroying it? It only happens to game objects that are connected to a prefab.

UPDATE 2: 01/05/11

So I am one step closer into finding out what the problem is. I took artm's function and edited it a bit:

public static void KillObjectsOfName(string name)
    GameObject victim;
    while(victim = GameObject.Find(name))

I use this function in two places, one before loading my xml and once afterward. It fails to function correctly if I load from an xml before or after running this function. However if I run this function alone without loading from an xml it excutes perfectly. Haven't found out why and so that is my next step.

A different solution: `EditorUtility.UnloadUnusedAssets();`. Calling this after `DestroyImmediate` that didn't seem to work makes sure the dangling objects are removed from the scene.

I do not want any of the original instances, I only want one in the scene at all times and I am loading the data for it from an xml

So create one and only update the prefab, don't create new instances. the dangling objects are probably temporaries from previous runs of the script, so the problem is that the script doesn't clean after itself. It should do something like:

        // this way links will persist when the mesh is regenerated
        Object prefab = AssetDatabase.LoadAssetAtPath (prefabPath, typeof(GameObject));
        if (!prefab) {
            prefab = EditorUtility.CreateEmptyPrefab (prefabPath);

        GameObject tmp = new GameObject(name);

        // ... set up your object from XML here (in tmp)

        // make sure prefab links remain in tact
        EditorUtility.ReplacePrefab (tmp, prefab, ReplacePrefabOptions.ReplaceNameBased);

        // get rid of the temporary object (otherwise it stays over in scene)
        Object.DestroyImmediate (tmp);

This script doesn't leave temporaries after itself, and only the object that you created from the prefab in the scene yourself will remain. Upon next run the original object will be updated by the script to the new version, which is the resultat you want. No?

Update. The problem seems to lie somewhere else. I made the following script:

using UnityEngine;

public class KillByName : MonoBehaviour {    
    [ContextMenu("Kill by name (GameObject)")]
    void Kill() {
        GameObject victim;
        while(victim = GameObject.Find("GameObject"))

It successfully kills all objects and prefabs called "GameObject" in one go (via context menu of an object I attach it to).

So after a long time of debugging and research I can finally answer my question. So the problem was, I was using trying to destroy objects that were connected to a prefab and right afterward use that prefab to create another object. When I use the reference of the prefab after trying to destroy game objects with a reference to the prefab, it canceled the destroy and would re-instantiate the game object I destroyed. I know it sounds strange but that is the easiest way to explain it. So I had to jump through hoops, create a temp object and use that as my reference instead so I could safely delete objects with references to a prefab.

Hopefully this will help others that run into a similar problem and good luck. Here is an expert of my code that fixed my problem:

        //Create temp object to use as a reference
        GameObject rGO = (GameObject)Object.Instantiate(replaceGO, replaceGO.transform.position, replaceGO.transform.localRotation); = "temp"; //name it something different just in case

        //kill any gameobjects in the scene with the same 
        //name as the game object I am loading from xml

        //get the name correct for the temp object = newTran.GetName();

        //create the object with xml data
        CreateObject(rGO, newTran.GetPosition(), newTran.GetRotation(), newTran.GetScale());

        //destory temp object