- Home /
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?
-Hektor
Answer by Jessy · Jan 14, 2011 at 02:56 PM
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];
}
Thank's now it works, but some other parts of my script do some strange things with CompareTag ... There are differences between the syntax and other.tag CompareTag?
Please click the link. There should be no functional difference, only performance.
Answer by 3dDude · Jan 14, 2011 at 02:09 PM
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?
Answer by vikitosss · May 11, 2016 at 08:13 AM
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];