- Home /
How to change the material of a sub-instance of one prefab ?
Hello everybody :)
I've an instantiated character prefab named "nounours" (a mechanic teddy bear). He's a biped, and he's composed of 3 elements :
Box002 (another mesh, the remote key of the tedy bear)
Bip001 (the biped system of the character exported from 3dsmax)
Box001 (the body of the teddy bear)
I want to change the material of this character. I've tried so many ways. For example I used some basic code:
var gremlins : Material;
function Update () {
///////Some code ............ ...... renderer.material = gremlins;
.......}
But it doesn't work. How can I access to the Box001 material (the mesh of this character) ? It doesn't work because I don't know the good way to change the material of Box001, which is a member of nounours. (the script of this material transformation is placed on "nounours")
Has anyone an idea ? A good way to do this ?
Thanks in advance for your suggestions and your help. :)
Answer by liszto · Nov 10, 2012 at 01:38 AM
Doing like him :
http://answers.unity3d.com/questions/180242/how-to-change-a-specific-material-of-an-object-at.html
But if you need to acces to your subprefab in your Nounours you must maybe do this in your start :
void Start()
{
Transform box001 = transform.Find("Nounours/Box001");
}
then use the same thing than my link in your update or wherever you want :
Material[] mats = box001.renderer.materials;
mats[1] = theNewMaterial;
box001.renderer.materials = mats;
if you have only one material use 0 and not 1 or maybe retry with material and not materials which is an array of multiple materials.
Hope this can help you.
You can do it recursively too. Recursive way can be used if you don't know the exact path of your object in your hierarchy.
If you want an example code I want write later for you, it's very simple and quick to implement (only 6 lines roughly)
Hi again Liszto, It would be very nice from you. I need to learn more. Thank you GA$$anonymous$$AGORIAN !
:o Gamagora ? How did you know it ? you're french ? ^^
Don't hesitate to post on Unity answer in the future I'm happy to help people ;)
Don't forget to give your solution and validate it or validate $$anonymous$$e. Good evening :)
I've seen your personal website. Yes i'm french and i'm a Gamagorian too ! Première promotion 2007/2008 équipe : Push the spankers. à bientôt.
Answer by JoeVoxel · Nov 10, 2012 at 11:41 AM
Hi Liszto,
thank you very much for your help. Finally I solved the problem yesterday . I don't know exactly how..but it works ! :). I puted the script on BOX001 and used a boolean in the Update function to activate the renderer material. The "renderer material" was called before this in a "OnCollisionEnter" function. It's seems that it was the problem. I'm going to try the way you show me, because it seem to be "cleaner" in coding terms. I've tried the Transform.find but i was wrong. I forgot to mention the path : like transform.FInd("Nounours/box001"); I think this was the problem.
thank you again :)
Your answer
Follow this Question
Related Questions
How to create a GUI button to change a character's texture in real-time? 2 Answers
Change material of all the children 2 Answers
Change Materials On Button Press or Hold? 1 Answer
How to change all materials of object and then change it back to normal? 0 Answers
How can I combine GUI button with script that is put on different gam objects? 1 Answer