• 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

3 Replies

· Add your reply
  • Sort: 
avatar image
11
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 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 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 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
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.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

75 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

Related Questions

Does Material.color, ParticleSystem.startcolor property clones/copies the material? 2 Answers

The best way to have multiple models with only one material 2 Answers

Unity collab keeps removing my material references 0 Answers

How to change object's material while preserving material's color? 0 Answers

Emissive Material control via script (code) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges