Shaders are triggering out of order.
I have a shader material and script applied to my enemy prefabs. There are three enemies in the scene. All have the same material and script applied. When the player object strikes the enemy, the shader triggers, dissolves, and the enemy disappears. The only problem is that they are activating in the wrong order. When the level resets (via a level loader) they shaders activate in the right order. But this "fix" does not apply to increased numbers of enemies. Any and all help appreciated,
Shader Script:
public class FadeOut : MonoBehaviour { Material material;
private bool isDissolving = false;
float fade = 0.5f;
void Start()
{
// Get a reference to the material
material = GetComponent<SpriteRenderer>().material;
}
public void FadeOutBool()
{
isDissolving = true;
}
void Update()
{
if (isDissolving)
{
fade -= Time.deltaTime;
if (fade <= 0f)
{
fade = 0f;
isDissolving = false;
}
// Set the property
material.SetFloat("_Fade", fade);
}
}
}
Here is the Enemy script:
public int maxHealth = 50; int currentHealth;
void Start()
{
currentHealth = maxHealth;
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
if(currentHealth <= 0)
{
Die();
}
}
public void Die()
{
FindObjectOfType<FadeOut>().FadeOutBool();
StartCoroutine(DeathWaiting());
}
IEnumerator DeathWaiting() { yield return new WaitForSeconds(0.5f); Destroy(gameObject);
}
}
Here is a Link showing the bug in action: https://www.youtube.com/watch?v=EVNY3L1S8MM
Thank you for any and all help.
Your answer

Follow this Question
Related Questions
How can I make an impact distortion effect? (Unity 2D),Impact distortion effect 0 Answers
How do you use different shaders with Unity 2019 2D lighting? 0 Answers
Need help getting started with a specific shader idea 0 Answers
Any way to create an 'Outer Glow' effect like this on text/UI elements? 0 Answers
See-Through effect for top-down game (Performance issues) 0 Answers