x


How do I render a prefab without any scripts/components running?

We have a number of prefabs that we instantiate as needed, each of which contain several scripts that automatically initialize them, send messages, register in lists, do fun stuff, etc.

However, I would like to render an "empty" copy of the prefab (i.e. just draw it without it doing any arbitrary stuff in the background), and I would like to know the best, safest, most reliable way to do this.

My first thought was to try to pull out all the meshes/renderers/animation components from the prefab, dynamically 'build' another, and use that, but I don't think there is any way to access the internals of the prefab without instantiating it (please correct me if that is wrong).

Assuming that is true, then I would need to instantiate the prefab. Is it possible to Instantiate() the prefab without running any of the code in the scripts, including their Awake() methods?

more ▼

asked Dec 31 '09 at 05:03 PM

Molix gravatar image

Molix
4.8k 15 27 66

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

7 answers: sort voted first

The problem is that if you instantiate a prefab right after the Instantiate() call Awake is called before you can do anything else. It's almost like the constructor. If that's not a big problem you can instantiate your prefab anddeactivate or remove the components you don't need / want like mentioned above. There's a nice way to remove or deactivate only the scripts of an GameObject:

MonoBehaviour[] Scripts = GetComponentsInChildren<MonoBehaviour>();
foreach (MonoBehaviour MB in Scripts)
{
    [...]

}

If the Awake would get you in trouble my approach would be to write an editor script that creates those preview-prefabs in the editor. Editorscripts are so powerful, half of my life is running in the Unityeditor. It's almost like you are in "The Matrix" :D

more ▼

answered Dec 28 '10 at 06:05 AM

Bunny83 gravatar image

Bunny83
45k 11 48 206

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

Use SetActiveRecursively(false) on the instantiated game object so it won't do any script processing.

more ▼

answered Nov 17 '10 at 12:12 AM

anomalous_underdog gravatar image

anomalous_underdog
335 3 6 18

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

What if instead of doing this at runtime you make an in-editor tool that preprocesses all of your prefabs and creates _empty versions? You could even write it in such a way that it maintains sync between the main and _empty prefabs. Probably. :)

more ▼

answered Dec 30 '10 at 02:50 AM

Ben Throop gravatar image

Ben Throop
259 5 9 13

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

As a supplament to Ashkan's answer.

If you want to make sure things are unchanged after you have done your stuff and you don't want to have to manually clean everything up, you can use RAII (Resource Acquisition Is Initialization) design pattern:

http://stackoverflow.com/questions/1757089/is-raii-safe-to-use-in-c-and-other-garbage-collecting-languages

The answer near the bottom by '280Z28' shows an example of how to do RAII in C#. It is a good way to change the state of the program/object temporarily. It also helps keep all the state modification code together making it more maintainable.

more ▼

answered Jul 02 '10 at 06:12 AM

_Petroz gravatar image

_Petroz
3.5k 27 34 57

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

Can you make a new prefab which is a "base prefab" that is the empty copy, and have that prefab included in the more complex one? Then changes to the base would still propagate to the scripted one, but you have more options about which one to instantiate. (Unless I'm remembering wrong, and you can't composite prefabs).

more ▼

answered Dec 31 '09 at 05:15 PM

MikeB gravatar image

MikeB
59 2 2 7

It's just an issue of quantity really; we have hundreds of prefabs so I would like a programmatic solution which does not require a significant alteration to the content pipeline.

Dec 31 '09 at 05:26 PM Molix
(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:

x5051
x1249
x275

asked: Dec 31 '09 at 05:03 PM

Seen: 2415 times

Last Updated: Dec 31 '09 at 05:03 PM