Delaying the execution of an 'If' statement in Unity C#

javascript delay execution
how to include delay in javascript
delay in java without using thread
java delay loop
settimeout execution context
call function with delay
delay javascript execution until page loaded
javascript delay callback

i am looking to execute a specific piece of code directly after the first one has finished and not at the same time like they are currently running.

private void Update()
{
    //This is the code to be executed first
    if ((textActive == true) && (stopText == false))
    {
        Debug.Log("TextActive");
        KeyText("On");
        objectToEnable4.SetActive(true);
        stopText = true;
    }    

    //after which this code will execute to disable Object4
    if (stopText == true)
    {

        objectToEnable4.SetActive(false);
    }
}

both pieces of code work perfectly i just need to implement a delay for the second code section i'm looking to delay the code for 2 seconds to allow time for an animation to play

thanks for the help in advance.


Good time to use a coroutine:

private void Update()
{
    //This is the code to be executed first
    if ((textActive == true) && (stopText == false))
    {
        Debug.Log("TextActive");
        KeyText("On");
        objectToEnable4.SetActive(true);
        stopText = true;
        StartCoroutine(myDelay());
    }
}

IEnumerator myDelay()
{
    // waits for two seconds before continuing
    yield return new WaitForSeconds(2f);

    if (stopText == true)
    {
        objectToEnable4.SetActive(false);
    }
}

How to delay the execution of a function in c#, Try using Task.Delay. You can use it like this: Task.Delay(1000).Wait(); DoSomething();. Hope it helps! It is true that justice delayed is justice denied; but more importantly, justice hurried is no justice at all. Hence, the Court should endeavour to strike an optimum balance between delay in execution of death penalty and the rights of the death row convicts. It would be exceedingly unfortunate if rights were compromised solely to decrease delay.


Base on the code that you have provided, I think they are doing just what you want it to do: Execute in order. However, because the code is very simple so it may seems like they are running at the same time, which make you can not see the objectToEnable4 at all. The way you can pause the execution is by using Unity's Coroutine.

The following code are the example of coroutine at Unity's Scritping API:

using UnityEngine;
using System.Collections;

public class WaitForSecondsExample : MonoBehaviour
{
    void Start()
    {
        StartCoroutine(Example());
    }

    IEnumerator Example()
    {
        print(Time.time);
        yield return new WaitForSecondsRealtime(5);
        print(Time.time);
    }
}

The first answer perfectly applied the corotine to your code. Hope you find it useful.

How to Delay Code Execution in Java, It is relatively common for Java programs to add a delay or pause in their operation. This can be useful for task pacing or to pause execution  Delaying Execution Device drivers often need to delay the execution of a particular piece of code for a period of time—usually to allow the hardware to accomplish some task. In … - Selection from Linux Device Drivers, Second Edition [Book]


imo there's an even 'better' way (at least, simpler way) using Invoke.

private void Update()
{
    if (textActive && !stopText)
    {
        KeyText("On");
        objectToEnable4.SetActive(true);
        stopText = true;
        Invoke("MyDelay", 2);
    }
}

private void MyDelay()
{
    if (stopText) 
    {
        objectToEnable4.SetActive(false);
    }
}

I'm not sure why you even need to use the bool stopText, maybe for something else you haven't shown us? If not you could remove that too!

delay the execution definition, delay the execution definition in the English Cobuild dictionary for learners, delay the execution meaning explained, see also 'delta',deadly',dearly',delayering',  One of this option is the possibility to delay of N seconds the execution of the script, I used time.sleep(N) but the script.py totally stops for N seconds, I just want to retard the script.sh of N seconds, letting the user continue using the script.py.


DELAY EXECUTION, Synonyms, crossword answers and other related words for DELAY EXECUTION [​stay]. We hope that the following list of synonyms for the word stay will help you  Rodney Reed CBS Austin The Innocence Project thanked Reed's supporters in a tweet on Friday. "Thank you to all who called, tweeted, and spoke out against the execution of an innocent person," the


Delay, Sleep, Pause, & Wait in JavaScript, Many programming languages have a sleep function that will delay a program's execution for a given number of seconds. This functionality is  The stay of execution by the Texas Court of Criminal Appeals came just hours after the Texas Board of Pardons and Paroles had recommended delaying the lethal injection.


sleep - Manual, Maybe obvious, but this my function to delay script execution using decimals for seconds (to mimic sleep(1.5) for example): <?php /** * Delays execution of the  That was the only other time, until Friday, that the parole board had recommended Mr. Abbott take action to delay or stop an execution. Manny Fernandez reported from Houston, and Richard A. Oppel