- Home /
Modifying several gameobjects with an editor script?
How can I change several objects in an scene from one to another? for instance lets suppose I have a ton of spheres and I want to change them all for cubes? and they are different objects? (not the same prefab)
Answer by Azrael · Mar 10, 2010 at 05:15 PM
Ok I finally figured this one by myself, so Im posting the answer for those who may be interested.
Short answer: you cant, You cant replace an object that is already on the scene with another, you can fake it, and get a pretty similar result though.
This is what you need to do
1.-Create a new prefab with the objects you want to "convert" from the scene (the cubes) lets call this "the list" 2.-Create another prefab with the final object you want to replace the originals(the sphere) lets call this "proxy" 3.-Now create an editor script that creates a new proxy in all the positions in the list (you can also copy other details like the script, textures etc)
//Select the list prefab in the assets var SelectedObject:GameObject=Selection.ActiveObject;
//Get the proxy var proxy:GameObject=Resources.Load("Proxy");
//Select all the transform in the list even if they are not active. for (var listObj:Transform in SelectedObject.GetComponentsInChildren(Transform, false){ if (listObj.Transform!=SelectedObject.transform){ Instantiate(proxy,myTransform.position, myTransform.rotation); }}
4.-Delete all the original objects from the scene.
Thats it. Is a bit of a laborious task but is better than modifying all objects by hand (specially if you have 800 like me, dont ask)
I hope someone finds this useful.
You might delete all the objects in the scene if they all have something in common by getting them by name or tag and putting them in an array. You can then iterate through the array and kill each one.
Answer by KristianHJ · Feb 04, 2012 at 02:15 PM
Just made a blog post about the subject here http://zaxisgames.blogspot.com/2012/02/batch-replace-gameobjects-in-unity.html