Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
5
Question by ptblk · Jul 21, 2015 at 07:17 AM · material colorchange material

Change material during runtime c#

Hi ive spent the entire day looking for a solution but can't find anything.

I have a chair (single mesh), with 3 materials on it, assigned to the 3 sub mesh components, as exported from max.

Now i want to change the color of lets say the white plastic from an interface palette, much like https://youtu.be/admsmCmaL3Y?t=55.

I started out basic with: if (Input.GetKey (KeyCode.A)) gameObject.GetComponent().material.color = Color.red

But this chooses a random material and changes that to red, so i cant specify which one relates to which From reading around, I found an array can possibly do this (the material change part, I know the palette GUI is a separate task), but I have no idea how to construct the full this script (artist not coder)

So far i found:

Material[] mats; mats = renderer.materials; mats[1] = theNewMaterial renderer.materials = mats;

But im not sure how to put this in the script correctly.

using UnityEngine; using System.Collections;

public class ChangeColor : MonoBehaviour {

 public Material[] mats;  

 void Update () 
 
 {
     mats = GetComponent<Renderer>().materials;
     mats[1] = theNewMaterial; 
     GetComponent<Renderer>().materials = mats;

 }

}

As much as I know about C# at the moment, thus a bit lost as I get error CS0103: The name `theNewMaterial' does not exist in the current context and not sure how to ref that properly

Then I would also be looking at how to do the swapping via the interface palette as a second objective :)

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

5 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by BiG · Jul 21, 2015 at 07:19 AM

The approach is correct, but theNewMaterial is an undefined variable. You should initialize it as an exposed variable, as you did with mats.

 public Material theNewMaterial;

Then, through the Inspector, drag and drop a Material from your project to the variable's value.

EDIT:

You can have three exposed variables identifing materials:

 public Material plastic;
 public Material wood;
 pubblic Material metal;

Then, when you have to assign them at the chair:

  mats = GetComponent<Renderer>().materials;
  //Here, you have to assign with the exact order!
  mats[0] = plastic; 
  mats[1] = wood;
  mats[2] = metal;
  GetComponent<Renderer>().materials = mats;
Comment
Add comment · Show 2 · 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 ptblk · Jul 21, 2015 at 09:50 AM 0
Share

Hi Big, thanks for the input, that worked, but it still just changes one of the components at random, in this case the legs to the new material.

How do I tell it which component refers to which material or set of material options that the user can choose from?

As I mentioned it has 3 materials on it, plastic, wood and metal. Single mesh

thanks

avatar image BiG · Jul 21, 2015 at 10:53 AM 0
Share

@ptblk: Hi, I have updated my answer. Since you are extracting an array of materials from the chair Object, you should "refill" it with all the possible values. I hope that my modified answer could help you a little bit.

avatar image
1

Answer by sas_88 · Jul 21, 2015 at 10:06 AM

Assign the materials in array as u did and get the materials present in the object n assign to it.

 count = 0;
 foreach(Renderer r in GetComponentsInChildren<Renderer>())
 {
     r.material=mats[count];
     count+=1;
 }
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 ptblk · Aug 03, 2015 at 11:29 AM 0
Share

Sorry can you be more specific. Assume I know very little which is true, so when you give me this code, i have very little idea of what to do with it. So far I have big's code as such:

using UnityEngine; using System.Collections;

public class ChangeColor : $$anonymous$$onoBehaviour { public $$anonymous$$aterial[] mats;
public $$anonymous$$aterial plastic; public $$anonymous$$aterial wood; public $$anonymous$$aterial metal;

 void Update () 
     
 {

     mats = GetComponent<Renderer>().materials;
     //Here, you have to assign with the exact order!
     mats[0] = plastic; 
     mats[1] = wood;
     mats[2] = metal;
     GetComponent<Renderer>().materials = mats;


 }

}

Although this doesnt really do anything except change the materials by default when I play the scene.

avatar image ptblk · Aug 03, 2015 at 11:32 AM 0
Share

What i need is that when i click on one of the 3 areas of the chair, that part will change material to one of the specified materials in the array if that makes sense. So if I click the plastic seat it will change and loop to the say 3 colors of plastic i have assigned.

Thanks

avatar image oliviergantois ptblk · Apr 01, 2016 at 10:20 AM 0
Share

Hi ptblk, I'm trying to do somthing similar, did you solve the problem? if yes could you show the full script? thanks

avatar image
1

Answer by unity_DrG5KGGUgbmWwA · Mar 11, 2018 at 08:18 PM

Hi all,

Is this chat still live, i have a similar question to finish off my project.

Your help would be most appriciated.

Thanks and hope to hear from you soon.

Curtis

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
0

Answer by jaspercayne · Apr 19, 2016 at 05:01 AM

Probably the best bet will be to raycast at the chair or create buttons that completely envelop the interchangeable parts. Then on a button press or a ray hit you would just set the current material to be the next one in the list, or if you are at the end of the list loop back around to the beginning again.

I can post something of an example of it is needed, but I take no responsibility for inefficient or unoptimized code.

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
0

Answer by MagyarPeter · Feb 06 at 12:21 PM

You cannot change the materials one by one, you have to replace the entire material[] array:

     public Renderer r;
     public Material newMaterial;
 
 void changeMat()
 {
 //this code creates a copy of the original material array, replaces the first element with the 
 //"newMaterial" then assigns the new amterial list to the renderer
             Material[] newMats = new Material[r.materials.Length];
             r.materials.CopyTo(test, 0);
             newMats[0] = newMaterial;
             r.materials = newMats;
 }

//I use this code to replace my character's materials when it dies, it should work

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

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to switch material with script? 1 Answer

Coloring of distant objects in No man's sky. 1 Answer

Colour not displaying properly ? 2 Answers

How Do I Partially Change The Material of An Object On Collision? 1 Answer

Change GameObject Color with Slider 0 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