1
votes

I have an object that I upgraded to the URP Sprite-Lit-Default material, but since I did that, the code I wrote to set the alpha color doesn't work at all. Upon further testing, I realized that it doesn't seem to work with red, green, blue, or alpha.

Color c = rend.material.color;
c.a = 120;
rend.material.color = c;

In the code above, rend references my sprite renderer. What do I have to do to change the color values from code on the upgraded material? I would like to use some lighting effects, so I don't wanna just switch to something else that doesn't support URP 2D lighting.

1

1 Answers

0
votes

Try this :

//Attach this script to any GameObject in your scene to spawn a cube and change the material color
using UnityEngine;

public class Example : MonoBehaviour
{
   void Start()
   {
       //Create a new cube primitive to set the color on
       GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);

       //Get the Renderer component from the new cube
       var cubeRenderer = cube.GetComponent<Renderer>();

       //Call SetColor using the shader property name "_Color" and setting the color to red
       cubeRenderer.material.SetColor("_Color", Color.red);
   }
}

https://docs.unity3d.com/ScriptReference/Material.SetColor.html