A little prototype I am making for my game, where the player's material should change when colliding with the white object.
gameObject.GetComponent<Renderer>().material = newMaterial;
This code is not working, I even tried to use this code at the start just to check if this works but doesn't. I have made sure to assign the newMaterial in Unity. I also tried changing color using gameObject.GetComponent<Renderer>().material.color = newMaterial.color;
but this also seems to not be having any effect on the player's color.
A little prototype I am making for my game, where the player's material should change when colliding with the white object.
gameObject.GetComponent<Renderer>().material = newMaterial;
This code is not working, I even tried to use this code at the start just to check if this works but doesn't. I have made sure to assign the newMaterial in Unity. I also tried changing color using gameObject.GetComponent<Renderer>().material.color = newMaterial.color;
but this also seems to not be having any effect on the player's color.
Not sure if you still need help, but I suggest you set a breakpoint in that line and check if the GetComponent actually finds your Renderer Component. My first guess would be, that it cant find it.
It would also be helpful to see a full example of your code, because the problem may lay somewhere else.
You can also try to execute this script on your game object and to see if there is something wrong with the components you assigned to your game object from inside the unity editor.
usingUnityEngine;
usingSystem.Collections;
publicclassExampleClass:MonoBehaviour
{
Material m_Material;
voidStart()
{
//Fetch the Material from the Renderer of the GameObject
This is an embed external element. It can be deleted using the delete key or the backspace key. To view the full element, press the preview button below.
Replies
unity material change after collision not working
Not sure if you still need help, but I suggest you set a breakpoint in that line and check if the GetComponent actually finds your Renderer Component. My first guess would be, that it cant find it.
It would also be helpful to see a full example of your code, because the problem may lay somewhere else.
You can also try to execute this script on your game object and to see if there is something wrong with the components you assigned to your game object from inside the unity editor.