- Home /
Boolean and input relation
Hello, i have script which checks the bool. But what i want is; i want to make boolean valid for some seconds so that i can press the keycode within that seconds of boolean valid and it will be verified by boolean and says , "you have enter correct keycode"
so i am looking for some seconds boolean valid after it gets true. If anyone has idea, please tell me.
Post the code you have written, and explain what doesn't work correctly.
Sounds like you need a timer... When you check for user input, you check first whether the time has elapsed, if not, then check to see if user has pressed enough keys for the password.
You could extend code from this tutorial:
https://www.youtube.com/watch?v=aHf96fBBY$$anonymous$$0
Have a go first, but if you're stuck for a few hours and you've checked online for other info, update your post with your new script and tell us where the problem is :)
okay, here is my code using UnityEngine; using System.Collections; using System.IO;
public class RandomLight : $$anonymous$$onoBehaviour {
public bool checkLights = true;
private const string FileName = "LeftLight.txt";
IEnumerator Start ()
{
checkLights = false;
while (true)
{
light.enabled =true; //toggle on/off the enabled property
yield return new WaitForSeconds(2);
checkLights = false;
light.enabled = false;
yield return new WaitForSeconds(Random.Range(3, 8));
checkLights = true;
if(checkLights)
{
if(!Input.Get$$anonymous$$ey($$anonymous$$eyCode.B))
{
if(File.Exists(FileName))
{
Debug.Log("File already exists");
}
StreamWriter sr = File.AppendText(FileName);
sr.WriteLine ( " You didnt press the right button " + System.DateTime.Now.ToString("yyyy.$$anonymous$$$$anonymous$$.dd hh:mm:ss"));
sr.Close();
}
}
}
}
}
so basically what this code does is, it checks the random light value and when it the bool finds the light enabled, i have to press the keycode immedately to get the response from the bool and it should valid that one. it works perfectly now and keycode also reponse if i press very fast but its very hard to press the keycode immedately. so, i want to make bool valid for some period of seconds so that the keycode which i press will be valid by bool.