How can i add a delay to pressing buttons in Unity - c#

I am very new to programming so please have patience with me. I am trying to add a delay to pressing a button in Unity. For example when i press "S" there should be a specified number of seconds that has to pass before i can press it again. Can anyone help me out ???

How can i add a delay to pressing buttons in Unity
If you want to achieve that you can set a delayTime and count it down in the Update Method and make the button do nothing on press until the time has passed.
Example:
float time = 0f;
void Update() {
if (time > 0f){
// Subtract the difference of the last time the method has been called
time -= Time.deltatime;
}
if (Input.GetKeyDown("S") && time <= 0) {
// Exectue Code you had in your Button Click function before
time = 2f;
}
}
If we want to elaborate on your answer we could also make the buttons enabled property disabled as long as the timer is not down to 0.
Example 2:
float time = 0f;
Button btn; // The Button you want to be unpressable
void Update() {
if (time > 0f){
// Subtract the difference of the last time the method has been called
time -= Time.deltatime;
btn.enabled = false;
}
else {
btn.enabled = true;
}
if (Input.GetKeyDown("S") && time <= 0) {
// Exectue Code you had in your Button Click function before
time = 2f;
}
}

I'm going to have to recall from memory here, but I believe you want to create an enumerator function, something along the lines of:
IEnumerator OnButtonClicked()
{
}
and when your button is clicked, you want to start this coroutine using:
StartCoroutine(OnButtonClicked());
And inside the OnButtonClicked() (or whatever you choose to name it), simply call:
yield return new WaitForSeconds(1);
and then do whatever you want it to do after the delay, after this.

Related

How to delay the else statement without coroutine in unity c#?

I have a joystick display picture for my game. Currently, when the player touches the screen the image disappears and when the player is not touching the screen, it reappears. I wrote that using an if else statement.
if (indicator.inputIndicator.x != 0)
{
joystick.SetActive(false);
}
else
{
joystick.SetActive(true);
}
The problem is, I want the image to reappear after some time like 2 seconds. I want to delay the "else", but I do not want to use a coroutine. I want "else" to work after 2 seconds since the player takes his hand off the screen but I couldn't figure out how to do it. any help will be great.
Setting a timer is a pretty common problem you have to solve in Unity. One basic approach is to have a variable that you add Time.deltaTime every update. That way you can tell how long it has been since some condition was met.
Every Update iteration that meets the condition, add Time.deltaTime to the variable. If at some point the condition fails, reset the variable to 0. Then you can just base your joystick.SetActive() call on the value of your variable.
For example, your script might become:
float thresholdTimeToShowPrompt = 2;
// By starting at the threshold, the image is hidden at the start until a touch
float timeSincePlayerTouch = 2;
void Update()
{
// Rather than calling SetActive directly, just update the timer
if (indicator.inputIndicator.x != 0)
{
timeSincePlayerTouch = 0;
}
else
{
timeSincePlayerTouch += Time.deltaTime;
}
// Now we can base visibility on the time since the last user touch
bool shouldShowIcon = timeSincePlayerTouch >= thresholdTimeToShowPrompt;
// Only call SetActive when needed, in case of overhead
if (shouldShowIcon && !joystick.activeSelf)
{
joystick.SetActive(true);
}
else if (!shouldShowIcon && joystick.activeSelf)
{
joystick.SetActive(false);
}
}

I'm trying to implement a time delay in Unity. I have done it using while loop, for loop and coroutines, but none of them seem to work

Here is my code.. I have explained the issue after..
public bool isPipeActive;
[SerializeField] private float timer;
void Start()
{
isPipeActive = false;
timer = 0f;
}
void FixedUpdate()
{
if (Input.GetKeyDown(KeyCode.Backspace))
{
isPipeActive = !isPipeActive; //bool flag invert
}
if (isPipeActive == true) //start pipe animations
{
for (int j = 0; j < ParentOf_arrow.transform.childCount; j++)
{
Initiate_arrow_FLOW_INDICATOR(j);
StartCoroutine(Time_Delay());
//while (timer < 1.0f)
for (timer = 0f; timer < 2.0f; timer += Time.fixedDeltaTime)
{
//just a blank function to see if it creates a delay..
timer += Time.fixedDeltaTime;
}
//timer = 0f;
}
}
}
IEnumerator Time_Delay()
{
yield return new WaitForSeconds(2f);
Debug.Log("2s delay initiated");
}
Initiate_arrow_FLOW_INDICATOR(j) visibly changes the color of one 3D arrow in a sequence of 3D arrows (arrow1, then arrow2, etc) to show direction. something like this.. i just want to add a time delay in between each color change, but none of the attempts i've made seem to create a time delay when i enter play mode and press Backspace key.
the colors flash extremely fast. There is no waiting between color changes whatsoever.. even when running the coroutine, while loop and for loop together :(
What is going on and how do i fix it??
The issue with the for loop is that you aren't waiting for the actual time between fixed updates and rather using the time between the last fixed update and adding it as fast as your CPU will until timer is larger than 2.
The coroutine will wait 2 seconds before logging it's output.
You need to put your arrow change code in the coroutine, because the Coroutine essentially works in "parallel" to your FixedUpdate.
So either do:
void FixedUpdate()
{
timer += Time.deltaTime;
if(timer >= 2.0f) {
//arrow thingy
timer = 0;
}
}
or in the Coroutine:
IEnumerator TimeDelay()
{
yield return new WaitForSeconds(2f);
//arrow thingy
}
In this state the FixedUpdate version will loop indefinitely and call arrow thingy roughly each two seconds, and the Coroutine will do so once.
A quick side note you should probably use Update and DeltaTime for this and not FixedUpdate with FixedDeltaTime.

Quit Unity3D Android app on 2 back button press

I would like my app to show the message on the first back button press as "Please touch back button again to quit the app" and when it is pressed again the app should quit. I think I have added appropriate code but it doesn't work.
The script is attached as a component to the canvas element.
The script contains the public variable which I assigned the panel(Child of canvas) UI element.
Scene hierarchy
Observed:
When I pressed the back button the text appears but only a fraction of a second and then disappear all of a sudden and the next back button press did not resulted in app quit.
Desired
On first back button press it should show the message and with in say 3 seconds if the second back button pressed the app should quit.
Relevant information:
Unity 2017.1.0f3
Here is the Code link :
https://gist.github.com/bmohanrajbit27/431221fc80e0b247649289fd136f9cfb
public class ChangeSceneScript : MonoBehaviour
{
private bool iQuit = false;
public GameObject quitobject;
void Update()
{
if (iQuit == true)
{
if (Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit();
}
}
if (Input.GetKeyDown(KeyCode.Escape))
{
quitobject.SetActive(true);
iQuit = true;
StartCoroutine(QuitingTimer());
}
}
IEnumerator QuitingTimer()
{
yield return new WaitForSeconds(3);
iQuit = false;
quitobject.SetActive(false);
}
}
I've seen few instances where Application.Quit(); did not work on Android. When this happens, use System.Diagnostics.Process.GetCurrentProcess().Kill(); to exit out of the program.
Now, for your timer issue, start a coroutine in the Update function when input is pressed for the first time. Use a flag to make sure that this coroutine function is not started again until the last one has finished. A boolean variable is fine.
Inside, that coroutine function, don't use yield return new WaitForSeconds(3); to wait for the timer. Use a while loop with the combination of yield return null; to wait until the timer is done. Increment the timer with Time.deltaTime each frame. Now, you can easily check for the second press in that coroutine function and exit if pressed.
If you also want this to work in the Editor, you have to use UnityEditor.EditorApplication.isPlaying = false; to exit. The example below should also work in the Editor. See the comments in the code if you have any question.
public GameObject quitobject;
private bool clickedBefore = false;
void Update()
{
//Check input for the first time
if (Input.GetKeyDown(KeyCode.Escape) && !clickedBefore)
{
Debug.Log("Back Button pressed for the first time");
//Set to false so that this input is not checked again. It will be checked in the coroutine function instead
clickedBefore = true;
//Activate Quit Object
quitobject.SetActive(true);
//Start quit timer
StartCoroutine(quitingTimer());
}
}
IEnumerator quitingTimer()
{
//Wait for a frame so that Input.GetKeyDown is no longer true
yield return null;
//3 seconds timer
const float timerTime = 3f;
float counter = 0;
while (counter < timerTime)
{
//Increment counter while it is < timer time(3)
counter += Time.deltaTime;
//Check if Input is pressed again while timer is running then quit/exit if is
if (Input.GetKeyDown(KeyCode.Escape))
{
Debug.Log("Back Button pressed for the second time. EXITING.....");
Quit();
}
//Wait for a frame so that Unity does not freeze
yield return null;
}
Debug.Log("Timer finished...Back Button was NOT pressed for the second time within: '" + timerTime + "' seconds");
//Timer has finished and NO QUIT(NO second press) so deactivate
quitobject.SetActive(false);
//Reset clickedBefore so that Input can be checked again in the Update function
clickedBefore = false;
}
void Quit()
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
//Application.Quit();
System.Diagnostics.Process.GetCurrentProcess().Kill();
#endif
}

Sprite not changing when key is pressed - unity 2D

Currently I'm simply trying to change the sprites candle from unlit to lit when the player has 'picked up' both the candle and the matches and the candle will 'go out' after a certain amount of time. However, when the space bar is pressed the transition from unlit to lit isn't occurring, even though the debug log is returning true when it should. I'm posting here to get some guidance as I have spent most of the day looking online and literally have no idea how to proceed.
Basically the images I am trying to transition between are two different images which are in the sprites folder under assets.
This is what I've got so far.
//the two sprites transition
public Sprite unlitCandle;
public Sprite litCandle;
private SpriteRenderer spriteRenderer;
bool pickUpMatches = false;
bool pickUpCandle = false;
float timeRemaining =5;
bool candleLit = false;
// Use this for initialization
void Start () {
spriteRenderer = GetComponent<SpriteRenderer>();
if (spriteRenderer.sprite == null)
spriteRenderer.sprite = unlitCandle;
}
// Update is called once per frame
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.CompareTag("Matches"))
{
collision.gameObject.SetActive(false);
pickUpMatches = true;
}
if (collision.gameObject.CompareTag("UnlitCandle"))
{
collision.gameObject.SetActive(true);
pickUpCandle = true;
}
}
public void CandleTimer()
{
if (candleLit == true)
{
timeRemaining = 5;
timeRemaining -= Time.deltaTime;
if (timeRemaining <= 0)
{
candleLit = false;
spriteRenderer.sprite = unlitCandle;
}
}
}
public void ChangeSprite()
{
if (spriteRenderer.sprite == unlitCandle)
{
spriteRenderer.sprite = litCandle;
}
}
void Update () {
if (pickUpCandle == true && pickUpMatches == true)
{
//Debug.Log(candleLit);
if (Input.GetKey(KeyCode.Space) && !candleLit)
{
CandleTimer();
ChangeSprite();
Debug.Log(timeRemaining);
candleLit = true;
//Debug.Log(candleLit);
}
}
}
}
Try comparing with a method like equals() instead of == in
spriteRenderer.sprite == unlitCandle
Because right now you are just comparing references and not the objects.
At least I think thats the problem.
There are a few possible issues with your code. First, you are calling changeSprite at the top of Update, which means that it is unconditionally being called every frame. Therefore, after a single frame of your candle being unlit, it will immediately change its sprite to litCandle.
I assume that the reason you are calling changeSprite every frame is in order to process the timer if you have a lit candle already. Really, you should move the code to process the timer (your whole second if statement in changeSprite) to a separate function and name it something like processCandleTimer. Call that at the top of Update and save the changeSprite method to only be called on the keypress.
Lastly, the issue that I suspect is giving you the most trouble is that you aren't resetting your timer, timeRemaining. The first time you light the candle the timer will go down to 0 after the 5 seconds pass. Every time changeSprite is run after that, you will change the sprite to litCandle in the first if statement and then immediately change it back to unlitCandle because the timer is 0 in the second. To remedy this, you need to add a line like timeRemaining = 5.0f; when the key is hit.

How To Create And Display A Timer In XNA Game Studio

I am creating a Board-Game style game in XNA Game Studio. One of the "mini-games" inside of it requires a timer. I have tried various methods but, I am stumped. If anyone can help, I would also like it to be activated by a button. This is the code I have currently:
if (orbStart)
{
int counter = 10;
int limit = 50;
float countDuration = 2f; //every 2s.
float currentTime = 0f;
currentTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
if (currentTime >= countDuration)
{
counter++;
currentTime -= countDuration;
}
if (counter >= limit)
{
//Stuff
}
}
}
OrbStart being the boolean activated by the button. Any help will be great!
Okay, first create global variables, for example:
bool isTimerOn = false;
float counter = 10; // 10 seconds
Then you should change isTimerOn to true when user presses your button, hope you know how to do it. Next step- inside your update method, substract elapsed time from the last tick from counter's value and check whether the time is up:
if (isTimerOn)
{
counter -= gameTime.ElapsedGameTime.Seconds;
if (counter <= 0)
doSomething();
}
Oh, I've noticed you wanted help with your button too. So first of all, you should create a Recentagle inside which your button will be placed:
Rectangle button = new Rectangle(50, 60, 100, 30);
This code means your buttons upper left point is located at (50, 60), it's width is 100 and it's height is 30. You can really read how to drawsprite at this location everywhere, as it was discussed for a couple of times here.
To check whether user clicks our button, just read about how to handle gestures and check whether tap's position is inside our rectangle, here's the code:
while (TouchPanel.IsGestureAvailable)
{
GestureSample gs = TouchPanel.ReadGesture();
switch (gs.GestureType)
{
case GestureType.Tap:
if (button.Contains((int) gs.Position.X, (int) gs.Position.Y))
isTimerOn = true;
break;
}
}
To make it work there's just one more thing- you have to enable tap gesture. Inside LoadContent() method add:
TouchPanel.EnabledGestures = GestureType.Tap;

Categories