1

I have animated my bird in blender and have imported that to Unity. I want to change the color of the bird via script so that I can change it from the settings panel.

What I noticed is that there is a .png file which is attached to the Base Map of the surface Input component under material component for the bird mesh.(Have attached the screenshot below).If I change the .png pic manually the bird color changes with the according variation in color I want. I want to change this via script so I wrote a small script as follows:

public class BirdMeshColor : MonoBehaviour
{
    // Start is called before the first frame update
    
    void Start()
    {    
        MeshRenderer meshRenderer = GetComponent<MeshRenderer>();    
        meshRenderer.material.SetTexture("_BaseMap", Resources.Load<Texture2D>("BirdColorsPics/greenBird.png"));

    }
}

But I get an error saying

"There is no 'MeshRenderer' attached to the "BirdMesh" game object, but a script is trying to access it. You probably need to add a MeshRenderer to the game object "BirdMesh"

I tried to run it in the script attached to the main bird as well and still got the same outputThe Bird's properties as seen in the Unity editor 1 I don't know what wrong I am doing. The Bird's properties as seen in the Unity editor 2

Chethan CV
  • 547
  • 1
  • 7
  • 23
  • What part do you not understand in `"There is no 'MeshRenderer' attached to the "BirdMesh" game object, but a script is trying to access it. You probably need to add a MeshRenderer to the game object "BirdMesh"` ? – derHugo Jun 25 '21 at 04:27

1 Answers1

1

"There is no 'MeshRenderer' attached to the "BirdMesh" game object, but a script is trying to access it. You probably need to add a MeshRenderer to the game object "BirdMesh"

As the error message explains you try to get the MeshRenderer component, but your object doesn't have a MeshRenderer, therefore you are getting the errors.

To fix that you can simply get the component that is actually attached to your bird instead, which in your case would be the SkinnedMeshRenderer.

Example:

void Start() {    
    SkinnedMeshRenderer skinnedMeshRenderer = GetComponent<SkinnedMeshRenderer>();    
    skinnedMeshRenderer.material
    .SetTexture("_BaseMap", Resources.Load<Texture2D>("BirdColorsPics/greenBird"));
}
IndieGameDev
  • 2,905
  • 3
  • 16
  • 29
  • 1
    Yeah, even I had tried to write SkinnedMeshRenderer earlier but it was throwing that red squirkly lines. Further more everything about your answer is right except that in the path .png should not be included – Chethan CV Jun 25 '21 at 03:30
  • Actually even better than that would be to use the parent class `Renderer` so your component could be used for both types – derHugo Jun 25 '21 at 04:28