- Home /
multiple meshes in a prefab and how to texture?
I expect I can place multiple meshes in a single prefab. And that the multiple meshes in the prefab will retain their position relative to each other as I rotate and display the prefab. Is this true?
In particular, I have a mesh of a space ship. I want to put an billboard (Unity plane) over each engine exhaust (there are 2 engine exhausts) and then place a texture with transparency on each billboard to simulate engine glow. I will update the engine glow texture every .05 seconds to get the flickering effect.
I do a 'renderer.material.mainTexture = shiptexture;' each frame to apply a texture to the ship mesh in the prefab.
My question is, if I have two billboards in the same prefab what is my code to apply a separate texture to each of the billboards (unity planes)?
Thanks for any help!
Answer by duck · Nov 27, 2010 at 08:23 AM
Each visible gameobject within your prefab will have its own renderer component, so you will have to find references to those components in your scripts in exactly the same way as you would any other component (even if it wasn't in a prefab).
For example, if your script that controls ship is also in charge of applying textures to these billboards, you might have something like this included in the script placed on your ship (assuming every ship has 2 exhausts, and they are named "Exhaust 1" and "Exhaust 2"):
var exhaust1 : Renderer; var exhaust2 : Renderer;
function Start() {
// we search the child objects connected to this ship to find the 2 exhausts:
exhaust1 = transform.Find("Exhaust 1").renderer;
exhaust2 = transform.Find("Exhaust 2").renderer;
}
function Update() {
// the code which you use to make the exhaust flicker will include this:...
exhaust1.material.maintexture = ....etc;
exhaust1.material.maintexture = ....etc;
}
Alternatively (and I prefer this method) you might consider making a separate "Exhaust" script, and place it on the exhaust objects directly. This simplifies things in that you can simply reference the renderer directly, so your entire "Exhaust" script might look like this, including the billboarding code and the code to modulate the engine glow texture:
var glowTextures : Texture2D[];
function Start() {
EngineFlicker();
// billboard loop - runs every frame:
while (true) {
transform.rotation = Camera.main.transform.rotation;
yield;
}
}
function EngineFlicker() {
// engine flicker loop - runs every half second:
while (true) {
renderer.material.mainTexture = glowTextures[Random.Range(0,glowTextures.Length)];
yield WaitForSeconds(0.5);
}
}
Looks good!
In your first example, with 'Exhaust 1' and 'Exhaust 2' how would these names be assigned to the billboard meshes?
Those would be the actual names of the gameobjects within your prefab. You have to name them before making the prefab - or modify them by placing the prefab back in the scene, modifying the version in the scene, then applying those changes back to the prefab.
ahh... ohh... Looks like I missing something big. I thought I could add multiple meshes to the same prefab but just tried it and I can not.
So if I want to add two billboards (Unity planes) to my prefab that holds the space ship mesh looks like I must create two seperate prefabs, one for each billboard/plane, yes?
I am starting to look for documentation or samples on how I connect the space ship prefab to the two billboard (plane) prefabs. I see some references to a hierarchy. How do I do this? If anyone can point me that would be great.
no, a prefab can contain multiple objects (each gameobject can have its own mesh). Your prefab can contain as many objects you like, parented in whatever hierarchical configuration that you like. You just can't change this arrangement without putting the prefab into the scene, making changes, then applying those changes back to the prefab.
Thank you for the info on how to apply textures to multiple meshes in one prefab.
I will need more instruction on how to put multiple meshes into one prefab and give each mesh a unique name. Would you be able to point me to documentation or tuturials that cover specifically how to do this? Thanks so much....!
Your answer

Follow this Question
Related Questions
Prefab Materials and Meshes being deleted, How can I stop this 1 Answer
duplicate a prefab then change the model? 1 Answer
how to have multiple meshes be part of a single instantiated prefab gameobject? 1 Answer
I can modify the GameObject transform using the inspector, how can I modify the mesh as well? 0 Answers
How to import a mesh NOT as a Prefab 2 Answers