Hi guys,
this is my first question here and I hope it’s not to noobish.
I try to adjust a material property to achieve a white flashing effect on a sprite.
It works fine if i do this:
private IEnumerator DoFlash()
{
thisRenderer.material.SetFloat("_FlashAmount", 1f);
Debug.Log("FlashOn");
yield return new WaitForSeconds(0.2f);
thisRenderer.material.SetFloat("_FlashAmount", 0f);
Debug.Log("FlashOff");
}
This does flash one time, so thats working fine. But if I want it to flash continuously like this:
private IEnumerator DoFlash()
{
thisRenderer.material.SetFloat("_FlashAmount", 1f);
Debug.Log("FlashOn");
yield return new WaitForSeconds(0.2f);
thisRenderer.material.SetFloat("_FlashAmount", 0f);
Debug.Log("FlashOff");
StartCoroutine("DoFlash");
}
… the _FlashAmount gets set to 1 and then it seems to simply stop working. The strange thing is that the Debug Statements showing up fine in the console, just like it should.
I just cant wrap my head around this. So if anyone has an idea I would greatly appreciate it.
here’s the whole script:
[RequireComponent(typeof(Movement))]
[RequireComponent(typeof(Rigidbody2D))]
public class KnockBack : MonoBehaviour {
private Rigidbody2D thisRigidbody;
private SpriteRenderer thisRenderer;
private Movement movementscript;
private SlowMotion slowMoScript;
private Vector2 direction;
void Awake ()
{
thisRigidbody = GetComponent<Rigidbody2D>();
movementscript = GetComponent<Movement>();
thisRenderer = GetComponentInChildren<SpriteRenderer>();
slowMoScript = Camera.main.GetComponent<SlowMotion>();
}
public void StartKnockback(Vector2 _direction)
{
direction = _direction;
StartCoroutine("DoKnockback");
}
private IEnumerator DoKnockback()
{
StartCoroutine("DoFlash");
gameObject.layer = LayerMask.NameToLayer("Invincible");
movementscript.SetCanMove(false);
thisRigidbody.AddForce(direction * 8f, ForceMode2D.Impulse);
GameManager.instance.SetPlayerIsInKnockBack(true);
yield return new WaitForSeconds(0.3f);
movementscript.SetCanMove(true);
yield return new WaitForSeconds(2f);
gameObject.layer = LayerMask.NameToLayer("Player");
GameManager.instance.SetPlayerIsInKnockBack(false);
StopCoroutine("DoFlash");
}
private IEnumerator DoFlash()
{
thisRenderer.material.SetFloat("_FlashAmount", 1f);
Debug.Log("FlashOn");
yield return new WaitForSeconds(0.2f);
thisRenderer.material.SetFloat("_FlashAmount", 0f);
Debug.Log("FlashOff");
StartCoroutine("DoFlash");
}
}