- Home /
[SOLVED] How can I modify the shader from the material of a single GameObject instead of all that are using the same material?
I'm trying to make an enemy desappear by using a shader effect, so what I need to do is basically change the "_Fade" value of the material so the enemy fades. The problem is that I want change the material of only one enemy, instead of all (since they're using the same material), but when I create a clone of the material in code, the "_Fade" value does not change by code anymore. What am I doing wrong? plzz
public Material EnemyMat;
float Timer = 2f;
int HP = 3;
void Start()
{
EnemyMat = Instantiate<Material>(GetComponent<SpriteRenderer>().material);
}
void Update()
{
Dead();
}
private void Dead()
{
if ( HP <= 0 )
{
Timer -= Time.deltaTime;
EnemyMat.SetFloat("_Fade", timer);
if (timer <= 0)
{
Destroy(this.gameObject);
}
}
}
captura-de-tela-2021-02-17-162953.png
(114.4 kB)
Comment
Answer by Mike2699 · Feb 17, 2021 at 08:04 PM
I don't exactly know why, but rather than using EnemyMat.SetFloat("_Fade, Timer);
, I used
GetComponent<SpriteRenderer>().material.SetFloat("_Fade", timer);
and worked!