- Home /
Question by
verschurengiovanni · Aug 06, 2020 at 07:09 PM ·
scripting problemscript.scripting beginner
IEnumerator Loop on keypress
Hello!
I'm trying to create a loop when a user presses a key. But i'm kinda stuck. it only loops 1 time and not everytime untill the user let's go of the key.
I'm not sure what i'm doing wrong and I need some help with this.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FlitsShade : MonoBehaviour
{
public Material material;
public static bool lightBool;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
lightBool = true;
StartCoroutine(Flits());
}
else
{
if (lightBool)
{
StartCoroutine(Flits());
lightBool = true;
}
else
StopCoroutine("Flits");
lightBool = false;
}
IEnumerator Flits()
{
while (lightBool)
{
material.EnableKeyword("_EMISSION");
yield return new WaitForSeconds(0.02f);
material.DisableKeyword("_EMISSION");
yield return new WaitForSeconds(0.02f);
}
}
}
}
Thanks!
Comment
Best Answer
Answer by cam74 · Aug 06, 2020 at 07:44 PM
Input.GetKeyDown()
is only called one time. Use Input.GetKey()
instead.