# Causing Update loop with Coroutine Unity

Total Noob Alert! < 7 days with C# and Unity

My Win Game screen becomes SetActive and my Player is SetActive false when you are standing in a trigger and crouch. That all works fine except you cant see the crouch animation because he is IMMEDIATELY SetActive false.

I tried to delay this by creating a coroutine. The problem with that is this is all happening in the Update function (or whatever it's called) because of the getkeydown for crouch. That gets called once per frame so my coroutine is trying to go off once per frame.

I tried creating a bool called isCoroutineStarted and set it to false and then set it to true in the Coroutine to break the loop but that didn't work either.

Don't know what to do so I reverted it back to before where it doesn't show my crouch animation.

Here's my script (It's attached to a Game Object with the trigger):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class xxxxxx : MonoBehaviour {

public bool xxx = false;

[SerializeField]
private GameObject youWinUI;

[SerializeField]
private GameObject player;

// Determines whether or not the player is standing on the xxxxx
void OnTriggerEnter2D (Collider2D other)
{
xxx = true;
}

void OnTriggerExit2D (Collider2D other)
{
xxx = false;
}

//Turns on Win screen and makes player disappear when crouch on xxx
void Update ()
{
if (Input.GetKeyDown (KeyCode.S) && xxx == true) {
youWinUI.SetActive (true);
player.SetActive (false);
}
}
}


P.S. ignore the x's no stealsies lol. Also this piece of script works as intended I just want a delay so you can see the crouch animation.

• Where is your crouch animation? Feb 11, 2017 at 14:17
• A trick I like to use is to save the Coroutine instance to a variable when starting a new coroutine, then reset it to null at the end of the routine. That way I can have other logic check if the coroutine is already running before trying to start it again, or manually stop just that one if it needs to be interrupted to transition into a new behaviour (without stopping all coroutines affecting the object) This answer has an example of this style of coroutine use Feb 11, 2017 at 18:01

You can use a trick:

While player is performed the crouch, you will increase a (float) counter value.

And when the desired amount has passed, you will disable the player.

Pseudo code will be like :

float counter = 0;
bool isCrouchStarted =false;

void Update ()
{
if (Input.GetKeyDown (KeyCode.S) && xxx == true)
{
youWinUI.SetActive (true);
isCrouchStarted = true;
}

if (isCrouchStarted == true)
{
counter +=0.1f;  //change the value for increase speed
}

if(counter>=1.0f) //change the value for desired time
{
player.setActive(false);
isCrouchStarted =false; // for not getting null ref. and increase value for no reason.
}

}


Hope this helps! Cheers!

• Don't forget to scale your counter increment by Time.deltaTime if you're running it in Update. Otherwise running with a fast framerate will trigger the transition sooner than you want, and running with a low framerate will trigger it later than you want. Feb 11, 2017 at 17:57
• That worked like a charm. I'll have to remember that one. Thanks EmreE!!! Feb 13, 2017 at 2:47