How to change the material of a sub-instance of one prefab ?

[4832-question+unity+material+change.jpg|4832]
alt text

Hello everybody :slight_smile:

I’ve an instantiated character prefab named “nounours” (a mechanic teddy bear). He’s a biped, and he’s composed of 3 elements :

  1. Box002 (another mesh, the remote key of the tedy bear)
  2. Bip001 (the biped system of the character exported from 3dsmax)
  3. 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.
:slight_smile:

Doing like him :

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.

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 :slight_smile: