- Home /
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;
}
}
}
}
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;
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!
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
Follow this Question
Related Questions
Material vs. SharedMaterial - difference in SETTING? 1 Answer
Keep replaced materials of a Game Object even after ending the Play Mode C# 0 Answers
How to share material between meshes 1 Answer
How to apply a color to the second texture in this shader? 1 Answer
Can't find unlit shader in material shader selection!, 1 Answer