Search code examples
unity-game-enginecomponentssprite

Unity SpriteRenderer set Material in script


I just want to set the material of my sprite renderer in my script. How can I do that? So I want to set the material of the Sprite Renderer in code but it doesn't work and I don't know why.


Solution

  • you can set the object/new material in the inspector by dragging and dropping your sprite renderer and the material to switch it to

    edit:

    public GameObject object;
    public Material newMaterial;
    //...
    object.GetComponent<SpriteRenderer>().material = newMaterial;