Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Molix · Dec 31, 2009 at 05:03 PM · prefabrenderer

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?

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MikeB · Dec 31, 2009 at 05:15 PM 0
Share

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).

avatar image Molix MikeB · Dec 31, 2009 at 05:26 PM 0
Share

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.

avatar image spree · Dec 31, 2009 at 05:18 PM 0
Share

I'd just create a second prefab and add a postfix _empty to it. This way you can instantiate it like any other prefab. Or is this not an option for some reason?

avatar image Molix spree · Dec 31, 2009 at 05:24 PM 0
Share

Really just a scaling issue -- we have hundreds of prefabs, and more on the way, so doubling the asset count and the maintenance required to keep them in sync just isn't practical.

avatar image Ben Throop · Dec 30, 2010 at 02:50 AM 0
Share

What if ins$$anonymous$$d 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. :)

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by anomalous_underdog · Nov 17, 2010 at 12:12 AM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Bunny83 · Dec 28, 2010 at 06:05 AM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Ashkan_gc · Dec 31, 2009 at 05:21 PM

first of all, you can easily change the components of prefabs before their instantiation. you should have a reference to them in your script. if you have that reference you can just use the GetComponent function and get it's components and do any other thing that you do with a normal gameobject. even you can change the transform but it's not in your scene and it will not render in any position. i don't know much about the implementation because the documentation says nothing about the engine internals. you can disable your scripts easily. let's assume that you have a prefab reference called "o" that is a GameObject. there is a script attached to it called "s", to disable this just right a code like this.

o.GetComponent<s>().enabled=false;

this is a C# code and the method is generic and can not be used on iphone. you should use the method with typeof or as keyword in iphone.

about drawing a mesh you can do different things. 1 if you have unity pro just use the graphics class methods to draw meshes. it's the fastest and most lightweight way to draw a mesh in unity. 2 you can create another prefab with new requirements and don't add script components and other components that you don't need and instantiate that prefab instead of the original one.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Molix · Dec 31, 2009 at 05:39 PM 0
Share

The Graphics.Draw$$anonymous$$esh stuff is in the right direction, but it requires a $$anonymous$$esh, which brings me back to needing to get at the stuff inside the prefab again :)

avatar image Ashkan_gc · Jan 01, 2010 at 07:37 AM 0
Share

what about disabling prefab components and instantiating it? then you can enable components of the prefab again.

avatar image _Petroz · Jul 02, 2010 at 06:12 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Acces a component on an instantiated object 1 Answer

Water splash issues... 2 Answers

Can we do this in Unity 3d? 1 Answer

How to create prefabs from Editor scripts? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges