- Home /
Perform Function, If Not Clicked In 3 Seconds
I have game that presents the user with a random number (between 1 and 6). Upon tapping on the number the game generates a new number. If the new number is the same as the previous number, and the user tapps on it, the player looses the game. there is also a timer that causes the player to loose the game if they do not tapp the current number within 3 seconds.
I would like it to be that if a duplicate number appears, the player must not tapp it, for three seconds, and then a new number generates, and the proccess starts again.
What I am looking for is some sort of 'IfNotClickedIn3Seconds' function. Thanks.
Answer by _Remu_ · Feb 01, 2015 at 09:26 PM
I solved it, by running a single timer. I set a bool variable where if true, something will happen if the timer reaches 0.01 seconds. If the bool is false, the timer will still reach 0.0 seconds, and time out the player.
Answer by Denvery · Jan 20, 2015 at 01:50 PM
May be you search for something like this?
float remainTime;
bool count3Seconds;
private void BeginCount3Seconds()
{
remainTime = 3;
count3Seconds = true;
}
private void StopCount3Seconds()
{
count3Seconds = false;
}
private bool IfNotClickedIn3Seconds()
{
remainTime -= Time.deltaTime;
if (remainTime < 0)
{
return true;
}
else
{
return false;
}
}
private void Update()
{
if (count3Seconds)
{
if (IfNotClickedIn3Seconds)
{
PerformYouFunc();
StopCount3Seconds();
}
}
}
(somewhere (after number generation may be) you need call BeginCount3Seconds()
)
As I promised, translated code to JavaScript:
var remainTime;
var count3Seconds;
function BeginCount3Seconds()
{
remainTime = 3;
count3Seconds = true;
}
function StopCount3Seconds()
{
count3Seconds = false;
}
function IfNotClickedIn3Seconds()
{
remainTime -= Time.deltaTime;
if (remainTime < 0)
{
return true;
}
else
{
return false;
}
}
function Update()
{
if (count3Seconds)
{
if (IfNotClickedIn3Seconds)
{
PerformYouFunc();
StopCount3Seconds();
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613180943im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Code to randomly generate a mesh? 0 Answers
Random spawn timer 1 Answer
Generate random path in grid 1 Answer
Using arrays to randomly select an object 0 Answers
Random Room Generator 1 Answer