Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
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 this :

 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. This 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 which 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 this 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 which keep track of any gameobject in it's hierarchy, then you can use that to access them. It's also possible to just loop through the children 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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Problem with using Vector3.MoveTowards on randomly instantiated target 1 Answer

assign different values to prefabs on instantiation 2 Answers

How to associate underlying data structure to prefab at runtime? 1 Answer

setting the text of an instantiated prefab's child's guitext object 1 Answer

Prefab instead of class oClass = new class() ? 1 Answer


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