- Home /
material Instance only updates after changes, when the Material Component is expanded or folded
I want to toggle the Shader Type and Alpha (Opaque to fade and Reverse) of a Default Material Instance at a GameObject, while runtime. The switching works after I selected the GameObject in the Editor and expand or fold the Material Component Tab.
Here is my Code: Do i need to update Something? The Interesting Part is void Open and void Close
private bool open = false;
public Animator animator;
public GameObject trainCabin;
private Material material;
private void Start()
{
material = trainCabin.GetComponent<Renderer>().material;
material.EnableKeyword("_Mode");
material.EnableKeyword("_Color");
}
public void OnSelect()
{
if (open)
{
Close();
}
else if (!open)
{
Open();
}
}
private void OnMouseDown()
{
if (open)
{
Close();
Debug.Log("close");
}
else if (!open)
{
Open();
Debug.Log("open");
}
open = !open;
}
private void Close()
{
animator.SetTrigger("door");
// trainCabin.GetComponent<Renderer>().material.SetFloat("_Mode", 0);
// trainCabin.GetComponent<Renderer>().material.SetColor("_Color", new Color(1f, 1f, 1f, 1f));
material.SetFloat("_Mode", 0);
material = trainCabin.GetComponent<Renderer>().material;
material.SetColor("_Color", new Color(1f, 1f, 1f, 1f));
}
private void Open()
{
animator.SetTrigger("door");
// trainCabin.GetComponent<Renderer>().material.SetFloat("_Mode", 2);
// trainCabin.GetComponent<Renderer>().material.SetColor("_Color", new Color(1f, 1f, 1f, 0.1f));
material.SetFloat("_Mode", 2);
material = trainCabin.GetComponent<Renderer>().material;
material.SetColor("_Color", new Color(1f, 1f, 1f, 0.1f));
}
Comment