Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 SuperScience · Feb 13, 2017 at 01:06 AM · materialmaterialsbug-perhapsfailureprogrammatically

Unable to set second material programmatically

Hi,

I need to set a second material to a gameobject programmatically.

But when I run...

 rendering.materials[1] = selectedMat;

... selectedMat is never applied to materials[1].

If I run again with the following debugs...

 Debug.Log("Expected: "+selectedMat.name);
 Debug.Log("Actual: "+rendering.materials[1]);

...I get the following:

Expected: OutlinedMaterial_Friendly

Actual: Default-Material (Instance) (UnityEngine.Material)

So Unity is failing to assign the requested material. Its somehow loading a completely different "default" material. I am able to assign the OutlinedMaterial_Friendly to Material slot 2 in the inspector, however, this is insufficient for my purposes. I need to be able to change this slot programmatically.

Any ideas on what I am doing incorrectly? Or is this a bug in Unity? 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

4 Replies

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

Answer by DSebJ · Feb 14, 2017 at 01:12 AM

You can't update the array of materials directly. You need to take a copy of it, update the position you want to change and then update the whole array of materials. I.e.:

 Material[] matArray = mesh.materials;
 matArray[1] = mat;
 mesh.materials = matArray;

Note: This looks like the expected behaviour, from the manual: "Note that like all arrays returned by Unity, this returns a copy of materials array. If you want to change some materials in it, get the value, change an entry and set materials back." - https://docs.unity3d.com/ScriptReference/Renderer-materials.html

Comment
Add comment · Show 4 · 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 SuperScience · Feb 15, 2017 at 05:07 AM 1
Share

You are amazing and I am a bit ashamed that I glossed over that note. Thanks for setting me right

avatar image DSebJ SuperScience · Feb 15, 2017 at 05:08 AM 0
Share

No problems, I was curious when I saw your post and thought it looked like it should work that way too :-)

avatar image jyblackshaw · Jan 20, 2021 at 03:21 AM 0
Share

If you want to change all the materials to your material:

if (renderer.materials.Length > 1) { for (int i = 0; i < matArray.Length; i++) { matArray[i] = my$$anonymous$$aterial; renderer.materials = matArray; } }

Also Dseb you saved me so much ttime tysm.

avatar image KarlKarl2000 · Dec 17, 2021 at 01:31 AM 0
Share

It works as coded above .. but I'm trying to do this in the editor (not at runtime).. which creates an INSTANCE of unchanged array elements.

ie: I change material element 1 (works fine), but the editor creates an INSTANCE of material element 0 ... there's a bit fat (Instance ) beside material array element 0 -- how can we revert that?

UPDATE used sharedMaterials to fix this instead of materials.. hope it helps others

avatar image
1

Answer by mefirstgames42 · Nov 14, 2020 at 09:08 PM

Hi there,

Try something like

 var mats = renderer.sharedMaterials;
 mats[materialIndex] = material;
 renderer.sharedMaterials = mats;


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 aditya · Feb 13, 2017 at 05:02 AM

you can set more than one materials from scripts only if you have more than one slots of material in inspector, you had to manually describe how many materials you want on your model (they should be equal to the number of UV sets your model has) .... Go to INSPECTOR > MESH RENDERER >MATERIALS and increase SIZE field

Comment
Add comment · Show 1 · 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 SuperScience · Feb 13, 2017 at 11:31 PM 0
Share

Hi Aditya,

Thank you for the response, but this is not the problem. I already have 2 slots for materials. If I didn't, the above code would have given an index out of range exception.

Remember that I am able to apply both textures in the inspector, so the problem is clearly not the way that the GameObject is organized. The problem must be in the script or in Unity itself.

avatar image
0

Answer by GoblinGameWorks · Apr 07 at 04:56 AM

I know the thread is a bit old but in case others come across this issue in 2022 +

@DSebJ 's answer above got me to my solution. I expect most people looking for this are trying to add an outline material to a character model (or similar 3D model).

  • you must have at least 2 materials on a mesh renderer or skinned mesh renderer component.

  • this script achieves replacing the mesh renderer's materials array when the mouse hovers over it. attach this script to the parent object of the 3d model in question.

  • Drag the 2 materials into the array slots in the inspector ( the ones that will replace the original ones when you hover over the object)

  • remember you are replacing the whole array not just 1 material.

    -------------------------------------------

       public class AddMaterial : MonoBehaviour
        {
     public Material[] newMaterials;
    
     Material[] originalMaterials;
     
     Renderer rend;
    
     private void Start()
     {
         rend = transform.GetChild(0).GetComponent<SkinnedMeshRenderer>();
         originalMaterials = rend.materials;
     }
    
     private void OnMouseOver()
     {
         rend.materials = newMaterials;
    
     }
     private void OnMouseExit()
     {
         rend.materials = originalMaterials;
     }
    

}

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

77 People are following this question.

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

Related Questions

Check if Material of a gameObject == a material variable (instance)? 2 Answers

Why Some Materials Make Trouble Get From 3dsMax and How to Solve It? 1 Answer

My foreach or the Resources.LoadAll Is not getting the information I need 2 Answers

Exporting textures from B2M3 to Unity 5 0 Answers

Is there a shader that cycles through textures 1 Answer


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