Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by AlastorSparda · Jul 28, 2011 at 05:07 PM · materialschange

Changing Material between multiple ones

I have a character with 3 materials, and i want to change it by code, i already have a script to change a material, is there a way to specify which material from my object i want to change?

Thanks

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by AlastorSparda · Jul 29, 2011 at 01:19 PM

Yup i did, but there seemed to be a bigger problem, i share with you my solution... i had to firt take the materials my mesh already had and save it on a var...


var Suits : Material[];

var Detail : Material[];

var currentSuitIndex : int = 0;

var currentDetailIndex : int = 0;

var currentSuit : Material;

var currentDetail : Material;

var currentSuitMat : Material[];

var ColorSuit : Color[] = [Color.red, Color.white, Color.blue, Color.green, Color.yellow, Color.black, Color.cyan, Color.magenta, Color.gray];

var currentSuitColorIndex : int = 0;

var currentDetailColorIndex : int = 0;

function Start ()

{

 currentSuitMat = renderer.materials;

 ActivateSuit(0);

 ChangeSuitColor(0);

 ChangeDetailColor(0);

}

function ActivateSuit(index : int)

{

 if (index >= Suits.Length)

 {

     index = 0;

 }

 currentSuitIndex = index;

 currentDetailIndex = index;

 currentSuit = Suits[currentSuitIndex];

 currentDetail = Detail[currentDetailIndex];

 currentSuitMat[1] = currentSuit;

 currentSuitMat[2] = currentDetail;

 renderer.materials = currentSuitMat;

}

function ChangeSuitColor(index : int)

{

 if (index >= ColorSuit.Length)

 {

     index = 0;

 }

 currentSuitColorIndex = index;

 currentSuitMat[1].SetColor("_Color", ColorSuit[index]);

 renderer.materials = currentSuitMat;

}

function ChangeDetailColor(index : int)

{

 if (index >= ColorSuit.Length)

 {

     index = 0;

 }

 currentDetailColorIndex = index;

 currentSuitMat[2].SetColor("_Color", ColorSuit[index]);

 renderer.materials = currentSuitMat;

}

function Update ()

{

 if (Input.GetKeyDown ("s"))

 {

     ActivateSuit(currentSuitIndex + 1);

 } 

 else if (Input.GetKeyDown ("d"))

 {

     ChangeSuitColor(currentSuitColorIndex + 1);

 } 

 else if (Input.GetKeyDown ("f"))

 {

     ChangeDetailColor(currentDetailColorIndex + 1);

 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by DaveA · Jul 28, 2011 at 05:09 PM

Yes you find the object within your character that has the material. If there is an array of materials, it will be indexed like any other array of things.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlastorSparda · Jul 28, 2011 at 06:08 PM 0
Share

Ok, i think i am doing something wrong, i used this and there is no error but nothing happend:

var Suits : $$anonymous$$aterial[]; var currentSuitIndex : int = 0; var currentSuit : $$anonymous$$aterial; function Start () { ActivateSuit(0); } function ActivateSuit(index : int) { if (index >= Suits.Length) { index = 0; } currentSuitIndex = index; currentSuit = Suits[currentSuitIndex]; renderer.shared$$anonymous$$aterials[1] = currentSuit; } function Update () { if (Input.Get$$anonymous$$eyDown ("s")) { ActivateSuit(currentSuitIndex + 1); } }

avatar image DaveA · Jul 28, 2011 at 08:16 PM 0
Share

Best to edit your question to put code there cuz you can format it as code there.

avatar image DaveA · Jul 28, 2011 at 08:19 PM 0
Share

Assu$$anonymous$$g the 'suit' mesh is at index 1, this looks ok to me. Of course you have assigned the materials to the Suits array, right? Are you sure you want shared$$anonymous$$aterial? http://unity3d.com/support/documentation/ScriptReference/Renderer-shared$$anonymous$$aterial.html

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to change all materials of object and then change it back to normal? 0 Answers

Removing Texture at Runtime Without replacing it 1 Answer

Changing specific materials onTriggerEnter 3 Answers

How do i change default material ? 1 Answer

How to replace materials in the materials array 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges