Unity 2018.3 HDRP - Change material color programatically?
I am using the new HDRP/LitTesselation shader.
I would like to change the Base Color + Opacity
at runtime:
I added this code to the game object's script:
void start()
{
Color color = new Color(0.5f, 0.5f, 0.1f, 0.8f);
//Fetch the Renderer from the GameObject
Renderer rend = GetComponent<Renderer>();
//Set the main Color of the Material to green
rend.material.shader = Shader.Find("_Color");
rend.material.SetColor("_Color", color);
}
But it generates an Hidden/InternalShaderError
error in the shader. Can anyone point me in the right direction?
Answer by henlit · Mar 16, 2019 at 05:00 PM
You are trying to find a Shader named "_Color" and set the shader to that, which probably dont exist. Try the name of the shader instead like this:
rend.material.shader = Shader.Find("HDRenderPipeline/Lit");
rend.material.SetColor("_Color", color);
Answer by raghadalghonaim · May 31, 2020 at 03:23 PM
Hello, is it possible by anyway to change the base map texture programmatically? thanks.
Hey, its late as hell but you change the base map like this
material.SetTexture("_BaseColor$$anonymous$$ap", _screenBlock[i].GetComponent<Camera>().targetTexture)
the latter in the SetTexture method is the name of your texture.
Answer by davidwhiffen · Jun 30, 2021 at 08:13 PM
This works for me finally
'red' can be Color.red or whatever you want.
public class MaterialTest : MonoBehaviour { [SerializeField] public Material myMaterial;
//public GameObject Sphere;
[SerializeField]
public Renderer rend;
[SerializeField]
public Color red = new Color(255, 0.4f, 0.6f, 255f);
// Start is called before the first frame update
void Start()
{
//Set the main Color of the Material to green
rend.material.shader = Shader.Find("HDRP/Lit");
rend.material.SetColor("_BaseColor", red);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612190133im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to load 3d model(.obj) in device by programmatic. 0 Answers
Applying shader for a disappearing object 0 Answers
Shader MVP matrices: what does vertex position mean at each step? 2 Answers
Unity 5 - Shader error in VertexBlend : Too many output registers declared (12). How to fix it? 1 Answer
How to get fragment X,Y,Z,W in a fragment shader using DX9 (Pixel Shader 2.0)? 0 Answers