x


Object Material change with script and a button?

I'm currently using this code so far :

//Change renderer's material each changeInterval
// seconds from the material array defined in the inspector.
var materials : Material[]; 
var changeInterval = 0.33; 
function Update () { 
if (materials.Length == 0) // do nothing if no materials
return; 
// material index 
var index : int = Time.time / changeInterval; 
// take a modulo with materials count so that animation repeats 
index = index % materials.Length; 
// assign it to the renderer
renderer.sharedMaterial = materials[index]; 
}

( Found from Unity Script Reference Help )

and it's working for switching the materials on my plane by a timer. I would like to know if there is a way to make this better by making the materials switch with a button such as a Mouse click or just a button press. Even better would be to figure out a way to click on a made button in the game for the "next material" button.

The furthest I figure is somehow making a counter.

Please help!

more ▼

asked Nov 11 '10 at 04:41 AM

Heather gravatar image

Heather
97 5 5 12

OK, to make this simpler, since I don't want to use a Gui....if I use a Input.GetKeyDown button instead...how would I make that??

Something like ..?

function Update() { if(Input.GetKeyDown("a").Button, "Next Material:)?

Nov 12 '10 at 09:16 PM Heather

ok, i fixed my answer, hope it helps!

Nov 15 '10 at 09:29 PM JesusChristChangedMe
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

i see what you want. ok, here is a script to say that when spacebar is hit to change the material next in the index. if you would like it to be left mouse click then use Fire1 instead of Jump in the script.

var myMaterials : Material[];
var NextMaterial : int = 0;
function NextMaterialUsed()
{
renderer.sharedMaterial = myMaterials[NextMaterial];
if(NextMaterial < myMaterials.length-1)
NextMaterial += 1;
else
NextMaterial = 0;
}

function Update () 
{
if(Input.GetButtonDown("Jump"))
{
NextMaterialUsed();
}


}

hope this help out!

more ▼

answered Nov 12 '10 at 02:06 AM

JesusChristChangedMe gravatar image

JesusChristChangedMe
479 11 14 23

Doesn't seem to work for me. I'm trying to avoid GUI's as much as possible since I have no idea how to use them.

Nov 12 '10 at 08:23 PM Heather

hmm... well i tired the script on a simple box i made and it worked fine. the only thing i can think of is if you didnt add the script to the object you want to change or you forgot to make the size number bigger under the script component and add new materials to it.

Nov 13 '10 at 10:28 PM JesusChristChangedMe

and if you wanted to change the color by pressing spacebar then you could do if(Input.GetButtonDown("Jump")) { //here you tell it to go to the next material. }

ill change my answer to fit what you want.

Nov 13 '10 at 10:31 PM JesusChristChangedMe

I managed to fix it the way I wanted a couple of days ago. Thanks for your help though, you got me most of the way. :)

Nov 15 '10 at 09:58 PM Heather

your very welcome! glad you fixed it!

Nov 15 '10 at 10:00 PM JesusChristChangedMe
(comments are locked)
10|3000 characters needed characters left
more ▼

answered Sep 15 '12 at 04:31 AM

cadviz gravatar image

cadviz
1 3 9 11

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

x3328
x813
x787
x100

asked: Nov 11 '10 at 04:41 AM

Seen: 3901 times

Last Updated: Sep 15 '12 at 04:31 AM