user9205278
user9205278

Reputation:

is there a way to delay an if statement/method in c#

So I have a problem with delaying a method from happening in a if statement, this if statement is in another method called OnCollisionExit(). The purpose of this mess is to try to prolong another method in another if statement in another method called OnCollisionStay().

I've made a timer, which actually works. The problem (I think) is that OnCollisionExit() runs through its code only one time... therefore the timer doesn't work (it doesn't reach zero). If you don't undrestand you will do when you read the code

timer method (it is run in the Update() method)

private void Counter()
    {
        if (counterEnabled)
        {
            remainingTime = remainingTime - 1 * Time.deltaTime;
        }
        if (remainingTime <= 0)
        {
            remainingTime = defaultTime;
            counterEnabled = false;
        }

    }

this is the OnCollisionExit() method

void OnCollisionExit(Collision collision)
    {
        counterEnabled = true;
        if (collision.gameObject == thing && counterEnabled == false)
        {
            //this is what am trying to delay
            DontDoSomething();
        }

    }

this is the OnCollisionStay() method

void OnCollisionStay(Collision collision)
    {
        if (collision.gameObject == thing)
        {
            //this is what Iam trying to prolong
            DoSomething();
        }
     }

if you don't know how the timer works... it works like this:

Upvotes: 1

Views: 217

Answers (1)

Nicklas C.
Nicklas C.

Reputation: 138

You could use a coroutine instead :

void OnCollisionExit(Collision collision)
{
        StartCoroutine (Countdown());
}

IEnumerator Countdown() 
{
    yield return new WaitForSeconds (2f);
    DontDoSomething ();
}

Upvotes: 2

Related Questions