I have a MeshRenderer mr
public variable in script1
. I dragged mesh1
from the assets in the mr
place in the inspector of script1
. mesh1
has a material mat1
, I try to change this material to mat2
using this mr.sharedMaterial = mat2;
but the material doesn't change. It only changes the material in the assets but not the one in the hierarchy, so in the runtime nothing changes. Any advice please?
Asked
Active
Viewed 1,916 times
0

Tak
- 143
- 2
- 10
-
I followed your steps. It works well. This is the script I assigned. // public class Test : MonoBehaviour { public MeshRenderer mr; public Material m2; void Awake(){ mr.sharedMaterial = m2;}} – Jinbom Heo Jun 04 '15 at 11:34
-
@JinbomHeo thanks for your comment. In my case the material in the assets is the one that is changed not the one is the hierarchy. Although the one in the hierarchy is a result of dragging and dropping the one in the assets. – Tak Jun 04 '15 at 11:37
1 Answers
1
Ok, I see.
I think your GameObject is a prefab which can not modified in the runtime.
So you can't apply the changes to other linked prefab objects in the runtime.
Only things that you can do is cloning and change the object's property respectively.

Jinbom Heo
- 862
- 1
- 6
- 15
-
thanks for your answer. Could you please tell me how to clone and change the object's property? I will upvote your answer anyway =) – Tak Jun 04 '15 at 11:58
-
1Ensure your MeshRenderer mr variable is pointing to the instance of the mesh renderer in the scene whose material you want to change, not to a prefab in your assets folder. You can create such an instance at runtime with the Instantiate() function. – DMGregory Jun 04 '15 at 14:09
-
-
@DMGregory but when I do
Instantiate
from the MeshRenderer mr, I find a new mesh appearing in different location in the scene :/ – Tak Jun 04 '15 at 17:03 -
1@shepherd: right, because Instantiate creates a new instance of the object. If you already have an instance in the scene, you want to point at that one. Try naming the scene instance something different from the prefab - eg if your prefab is called "MyMesh", call the instance of MyMesh in your scene hierarchy "MyMeshInstance" - this will make it easier to tell which version your script variable is pointing at. – DMGregory Jun 04 '15 at 17:08
-
@DMGregory Okay, so if my prefab mesh is called
MyMesh
and the instance in the hierarchy is calledMyMeshInstance
. After using instantiate it creates another instance in the hierarachy calledMyMesh (clone)
which appears in the scene in different location thanMyMeshInstance
. And when I change the material ofMyMesh (clone)
it changes its material. But what I want to do is to change the material ofMyMeshInstance
notMyMesh (clone)
norMyMesh
. Any advice please? – Tak Jun 04 '15 at 17:18 -
@shepherd If you already have an instance in your scene, you don't want to use Instantiate(). As I said above, that's only if you want to create a new instance at runtime. The change you need to make is to set your script variable mr to point at MyMeshInstance's MeshRenderer before trying to change its material. Comments aren't great for extended discussion, so if you have further trouble with this let's hop over to the chatroom. – DMGregory Jun 04 '15 at 17:22