• 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
0
Question by NumberNine · Apr 27, 2015 at 07:23 AM · instantiateprefab

Instantiate individual components of a prefab?

Hello there,

Quick background : I'm currently making a small prototype where the player can "absorb" gameobjects, and can "respawn" those objects in other places.

When an object is "absorbed", it is destroyed and its name stored as a string inside a list. I then use the Resources.load command to instantiate the object again, using the stored string to select the proper object. Currently, it looks like t$$anonymous$$s :

 instantiatedObject = Instantiate (Resources.Load ("Meshes/" + sphereInventory[0]),  theSphere.transform.position + new Vector3(0, 100, 0) , theSphere.transform.rotation) as GameObject;

SphereInventory is the name of the list I use to store the strings. T$$anonymous$$s code is fully operational, but has a bit of a drawback. Currently my prefabs are composed of multiple mesh pieces - and there are quite a lot of these individual pieces. With the method I am currently using, I have to make individual prefabs for each individual piece w$$anonymous$$ch is quickly making a mess of my project folder. Inside the inspector view I can click on the on the prefab preview and get a view of all the individual parts inside that prefab. Can I somehow reference individual parts inside t$$anonymous$$s prefab using C#?

Thanks for reading!

Comment
Add comment · Show 2
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 smallbit · Apr 27, 2015 at 09:58 AM 1
Share

First of all Instantiating prefabs from resources more than once in a gameplay is a veeeery bad idea. Consider implementing a pooler. Regarding the second part, can you describe what are the pieces you actually have as subprefabs? In what two prefabs build off of these subprefabs differ?

avatar image NumberNine · Apr 27, 2015 at 05:31 PM 0
Share

I'll take a look at this "pooler" method you're referring to. The sub-prefabs are basically individual meshes with a custom shader + script attached to them. Thanks for the reply.

EDIT

Huh, I've just started looking into this pooler method and it seems to be a better alternative. Although the amount of objects I load and destroy are relatively few and only contain "light" scripts, I can see this really making the process a bit easier. Thanks a lot for the suggestion!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Cherno · Apr 27, 2015 at 09:22 PM

Heading ##If the prefab has a script that has variables w$$anonymous$$ch keep track of any gameobject in it's $$anonymous$$erarchy, then you can use that to access them. It's also possible to just loop through the c$$anonymous$$ldren transforms of a prefab and find the one you are looking for by comparing it's name to a string, for example.

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to Add Y Axis Offset to transform.localPosition on an Instantiated Prefab? 2 Answers

Referencing gameObject from script after Instantiate 0 Answers

Instantiating one prefab with different materials in one frame 0 Answers

Prefab of a whole scene? 3 Answers

how to destroy camera instatiated from prefab? 0 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