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(replaceObject.name);
while(oldGO != null && oldGO.name != "")
oldGO = GameObject.Find(replaceObject.name);
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
Step 2- clicked load xml again, destroys remaining game objects of name "test"
Step 3- now all game objects named test are deleted
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.
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)
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.