why are not objects (materials) by reference in unity?
Hi. In c# all objects of the classes are the reference. In other words if you assign ClassA class2=new ClassA(); ClassA class1=classa2; class1.a=10; then if you change class1 you see class2 changes too but in unity if you change a material object (mat2) you see the other material (mat1) will not change!! Renderer renderer=GetComponent(); Material mat1=renderer.material; Material mat2=mat1;
Answer by NoseKills · Jul 01, 2016 at 06:13 AM
Materials like all classes are reference types so they are passed by reference. You can easily test this with the example you provided yourself.
Material materialA = new Material(Shader.Find("Standard"));
Material materialB = materialA;
materialA.color = Color.red;
Debug.Log(materialA.color);
Debug.Log(materialB.color);
Renderer.material however is a property that clones the material if it's used in multiple places ( as mentioned in the API docs)
Use Renderer.sharedMaterial if you don't want a clone
Your answer
Follow this Question
Related Questions
Black Material when camera rotate 1 Answer
Why is my material not changing? 3 Answers
I want to switch a game object's material back to its original after its been altered?!?!?! 1 Answer
Problem with fresnel materials 0 Answers
How do I modify a 'Specific' Material in my gameObject's Renderer? 0 Answers