- Home /
Renderer.material.color not changing color of prefab
Hello everyone,
my use case if very simple: I want the color of an object to change when it is clicked. If the user clicks somewhere else, it gets deselected and the color switches back to the original one. This already works with a cube for me by the simple code of:
activeObject.GetComponent<Renderer>().material.color = Color.red;
Now I was importing an .obj file to make a prefab out of the mesh. If I click on the imported .obj it consists of a gameobject, a mesh and also a Material. I dragged the import into the scene, added some other components e.g. a meshrenderer and made it a prefab. The prefab also has the script with the line above for color changing. Selecting and deselecting the object works, however the mesh remains white-ish instead of turning red when active.
In the inspector, I assigned a defaul material (instance) to the materials/Element0 of the meshRenderer component component. When I now click on my object, the materialin the inspector also turns red as seen on the little colored sphere displayed. However in game the object still is white. What am I missing here? Thank you in advance!
Answer by JustAnotherRobot · May 28, 2021 at 04:27 PM
I found out what the problem was and it probably is a real beginner's mistake. By dragging the imported .obj into the scene I got an empty parent gameobject named like the model with the actual model attached as a child. From my code I was just changing the parent's material. I adapted my code to first get the child component and then set it's material and everything works as expected now.
Answer by EDDDDDDD · May 28, 2021 at 01:06 PM
Hi, what about changing the material of the object entirely. so activeObject.GetComponent().material. = . I suggest doing this as you won't have any issues with the default material not having a colour that you can edit.
There will be different ways of achieving and solving this issue. This is just the simplest way I can explain a solution to this.
Cheers
Thank you for your response! I tried out the line
activeObject.GetComponent().material =Color.red;
However this gives me an error that the type argument cannot be inferred from the usage. If I add an after GetComponent I still receive an error. I also tried setting the Line to a Material which I defined over a public member variable. Still doesn't work. Adding the line activeObject.GetComponent<Renderer>().material = Mat
changes the renderer's material to the new material I assigned but the gameobject itself still is colored white.
Answer by Harry_Drew · May 28, 2021 at 02:10 PM
Hi, i think i realise the problem: activeObject.GetComponent(Renderer).material = Mat should be changed to activeObject.GetComponent(MeshRenderer).material = Mat. Make sure that the material that you assign in the inspector has the colour red.
Please let me know if this causes any issues and I will see what I can do. Cheers
Your answer
Follow this Question
Related Questions
Does Material.color, ParticleSystem.startcolor property clones/copies the material? 2 Answers
GUI button texture different color when script is on Instantiated object 0 Answers
Assign different materials to a range of same prefab? 1 Answer
How to change material of the active object? 1 Answer
My foreach or the Resources.LoadAll Is not getting the information I need 2 Answers