x


How to change a gameObject's material via scripting?

How would I change a gameObject's material via scripting?

more ▼

asked Jul 29 '10 at 12:30 AM

MikezNesh gravatar image

MikezNesh
843 64 74 93

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Use:

var texture : Texture;

function Start () {
    renderer.material.mainTexture = texture;
}

To change the texture to the material exposed in the inspector. For further material and texture documentation follow this link.

more ▼

answered Jul 29 '10 at 12:34 AM

e.bonneville gravatar image

e.bonneville
5.7k 100 116 165

That doesn't really change the material.

Jul 29 '10 at 12:38 AM Magnus Wolffelt

What do you mean? It's from the docs.

Jul 29 '10 at 12:51 AM e.bonneville

I guess it depends on what you mean by change. My understanding of the question was that the author wanted to exchange the material, not just modify it.

Jul 29 '10 at 09:38 AM Magnus Wolffelt

I think he just wanted to have a different texture.

Jul 29 '10 at 11:33 AM e.bonneville

Ya. thanks to both of you guys. sorry I didn't write my question clearly.

Jul 29 '10 at 06:01 PM MikezNesh
(comments are locked)
10|3000 characters needed characters left

First of all, a GameObject can't have a material. It can, however, have a Renderer, which has a Material: http://unity3d.com/support/documentation/ScriptReference/Renderer-material.html

So basically:

renderer.material = myOtherMaterial;
more ▼

answered Jul 29 '10 at 12:37 AM

Magnus Wolffelt gravatar image

Magnus Wolffelt
457 9 11 26

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5056
x2074
x807
x379
x7

asked: Jul 29 '10 at 12:30 AM

Seen: 13872 times

Last Updated: Jul 29 '10 at 12:30 AM