• 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 chris3331 · Jun 09, 2015 at 09:52 PM · materialmaterialsreplacesharedmaterial

Replacing sharedMaterials by an existing Material C#

Hello, My question is why this is working and changing the color:

 gameObject.GetComponent<Renderer> ().sharedMaterials[1].color = Color.blue;

But replaced by this it isn´t changing the material:

 gameObject.GetComponent<Renderer> ().sharedMaterials[1] = Resources.Load ("NewMaterial/TestMat") as Material;

or this;

 Material NewMat = new Material (Resources.Load ("NewMaterial/TestMat") as Material);
 Debug.Log (NewMat); // working!!
 gameObject.GetComponent<Renderer> ().sharedMaterials[1] = NewMat;

I don´t get an error and the material "TestMat" is stored in the right folder! As an Unity-Beginner i probably overlooked something obvious! Thanks for helping!!!


for better understanding

 using UnityEngine;
 using System.Collections;
 
 public class SetMaterialToo_04 : MonoBehaviour 
 {
     
     void Start() {
 
         Material[] materials = gameObject.GetComponent<Renderer> ().sharedMaterials;
         int Arraylength = materials.Length;
         Debug.Log (Arraylength);
 
         for (int i = 0; i < Arraylength; i++) {
             string matName = materials [i].name;
         
 
             if (matName == "Test_rot") { 
 
                 int var = i;
                 Debug.Log (materials [var]);
 
                 Material NewMat = new Material (Resources.Load ("NewMaterial/TestMat") as Material);
                 Debug.Log (NewMat); // working!!
                 gameObject.GetComponent<Renderer> ().sharedMaterials [1] = NewMat;
 
             
             }
         }
     }
 }
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

1 Reply

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

Answer by maccabbe · Jun 10, 2015 at 02:23 AM

gameObject.GetComponent ().sharedMaterials and gameObject.GetComponent ().materials return a copy of the array that stores the materials.

 mesh.materials = {mat1}
 copyMaterials = mesh.materials
 so
 copyMaterials = {mat1}

So we do not expect that editing the copy to affect the original.

 copyMaterials = {newMat}
 but
 mesh.materials = {mat1}

In addition, the copy of the array of materials stores the original materials. So editing the materials contained in the copy of the array also edits the materials contained in the original array

 copyMaterials.mat1.color=newColor
 so
 mesh.materials = {mat1} (where mat1 has had a new color assigned)

However you can change mesh.materials[] by assigning to it

 Material[] materials=gameObject.GetComponent<Renderer> ().sharedMaterials;
 materials[1]=Resources.Load ("NewMaterial/TestMat") as Material;
 gameObject.GetComponent<Renderer> ().sharedMaterials=materials;




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 chris3331 · Jun 11, 2015 at 11:52 AM 0
Share

Thank you very much! But what if I want to keep the changed materials even after ending the Play $$anonymous$$ode? At the moment all changes are resetting after ending the Play $$anonymous$$ode but I would like to change the existing materials of the Object forever!

avatar image OnyDeus · Jun 26, 2015 at 01:18 AM 1
Share

I beleive if you want the changes to be permanent you should look into [ExecuteInEdit$$anonymous$$ode] (and thank you for asking I was banging my head and should have known better)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Keep replaced materials of a Game Object even after ending the Play Mode C# 0 Answers

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

Emissive Material control via script (code) 1 Answer

Setting a material texture and saving it 1 Answer

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