Changing specific materials onTriggerEnter

Hello everyone. I wanted to make a script (in Javascript) who changes some specific materials of the material array of an object, but I don't know the right syntax. Here's my script:

function OnTriggerEnter (other : Collider) 
    if(other.tag  == "Sphere") 
    {renderer.materials[0] = newMat;
    renderer.materials[1] = newMat2;}

Can someone give me the proper way to do it please?


You cannot assign to an individual element of the materials array. You have to do it all at once. Also, use CompareTag.

You may want to edit this question, because your problem has nothing to do with triggers.

function OnTriggerEnter (other : Collider) {
    if (other.CompareTag("Sphere")) renderer.materials = [newMat, newMat2];

Does this not work? Did you try it? It looks correct to me, But I might be overlooking something. What is the error(s) you are getting if any?

Hi, yes this not work

var newMat : Material;

v1. gameObject.GetComponent.<Renderer>().materials[0] = newMat;
v2. gameObject.GetComponent(Renderer).materials[0] = newMat;

DebLog Answer :
wood2 (Instance) (UnityEngine.Material)

but the material, is not changed…

its work if only one will be changed:

v3. gameObject.GetComponent(Renderer).materials = newMat;

How to change if I have more materials ?

I did. its working if don’t only one material will be changed
very simple:
gameObject.GetComponent.().materials = [newMat1, newMat2];