- Home /
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!
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?
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!
Answer by Cherno · Apr 27, 2015 at 09:22 PM