First of all thank you Unity3d team for your great work! But today I got stuck on a little problem. I’ve tried to change materials via script. My first idea was something like this
private var object : GameObject;
function setTarget(newTarget) {
object = GameObject.Find("char");
}
function changeTextures(newTexture) {
object.renderer.material.mainTexture("myJPG.jpg");
}
If I use this methods nothing happens at all. Also if I change the color of a material via material r,g,b,a I get pretty abnormal results. Picked red and placed the numbers into this method an the result is white?
public function setColorRGB(x,y,z,i) {
object.renderer.material.color.r=x;
object.renderer.material.color.g=y;
object.renderer.material.color.b=z;
object.renderer.material.color.a=i;
}
If somebody could just give me a hint? Also I would be very thankfull for a short idea how to change materials via js.
Thanks
Check on the documentation for this stuff. The color is pretty easy, but you can’t assign a texture by name as far as I know.
var texture : Texture;
object.renderer.material.mainTexture = texture;
object.renderer.material.color Color.red;
object.renderer.material.color = Color(r, g, b, a);
Changing the color does work actually with my script. it is just acting weird. i developed a function which returns the color settings of an object and recognized that the rgb spectrum is a little bit confusing in unity. but far away from that I still need to know if there is way to change textures or materials via script. mainTexture property is not working. oh and posted the wrong code sorry for that
here is the right one.
private var object : GameObject;
function setTarget(newTarget) {
object = GameObject.Find("char");
}
function changeTextures(newTexture) {
object.renderer.material.mainTexture(newTexture);
}
The mainTexture property works fine. You’re not defining what newTexture is with your code snippet.
Create an variable for it just as BigMisterB showed you. You then need to drag your desired texture to the slot in the inspector to link it with the variable.