- Home /
Not allowed to access Renderer.material on prefab object?
Im spawning tables, chairs and other objects from prefabs and I would like to change their color at runtime.
selectedMesh.GetComponent<MeshRenderer> ().material.color = colors [value];
But I get the error "Not allowed to access Renderer.material on prefab object. Use Renderer.sharedMaterial instead"
But I dont want to use sharedMaterial, since it changes the material for all the objects that share that material. I instead want to change the specific material of that single object.
Thanks for helping.
Answer by hexagonius · Jun 30, 2017 at 02:52 PM
selectedMesh is not the object in the scene, meaning not an instantiated object. It's the prefab itself you're accessing, the one sitting in the project folder. Change that and it'll work.
Could you tell me how?
Currently I provide the Prefab object via drag & drop onto a script which holds the prefab for me. (Is used for dummy objects which just represent the objects without any attachments),
Later on I access that prefab reference by :
g.GetComponent<ActualPrefabHolder> ().actualPrefab;
and I instanciate the GameObject like this:
selectedObject = GameObject.Instantiate (go) as GameObject;
I dont know how Im supposed to unlink the newly created GameObject with the prefab. I tryed GameObject -> Break Prefab Instance in the $$anonymous$$enu; didnt work. And I did PrefabUtility.DisconnectPrefabInstance(selectedObject) ; which also didnt work.
Thanks for helping!
You wrote selected$$anonymous$$esh. Now it's only getting up to selectedObject. With that you should be working alright.
Oooohh I see what I did wrong, thanks! I asigned the actual prefab to my ItemHandler ins$$anonymous$$d of the Instantiated Object. Solved the problem.
This is a good answer, I assigned a mesh to "blinking" when being damaged. Took 2 months to figure the above error warning. Thank you so much.
Answer by sniper02311 · Mar 16, 2019 at 04:23 AM
With Instantiate(Cube, new Vector3(x, y, 0), Quaternion.identity);
You have to make that a gameobject (i.e. object "in the game itself") by changing it to:
GameObject [whatever] = Instantiate(Cube, new Vector3(x, y, 0), Quaternion.identity) as GameObject;
THen under ChangeColor1(), say:
[whatever].GetComponent().material.color = Color.red;
That should work. Then its referencing the object that was instantiated, not the prefab it was instantiated from.
Your answer
![](https://koobas.hobune.stream/wayback/20220612125217im_/https://answers.unity.com/themes/thub/images/avi.jpg)