x


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)

more ▼

asked Mar 09 '10 at 10:17 PM

Azrael gravatar image

Azrael
233 5 8 9

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Mar 10 '10 at 05:15 PM

Azrael gravatar image

Azrael
233 5 8 9

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.

Apr 07 '10 at 05:38 PM e.bonneville
(comments are locked)
10|3000 characters needed characters left
more ▼

answered Feb 04 '12 at 02:15 PM

KristianHJ gravatar image

KristianHJ
61

If I could upvote you, I would!

Aug 10 '12 at 08:01 AM Bomadeno

Np, but great it could help you

Aug 10 '12 at 08:24 AM KristianHJ
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3418
x2170
x1728
x389

asked: Mar 09 '10 at 10:17 PM

Seen: 2183 times

Last Updated: Aug 10 '12 at 08:24 AM