So apparently Unity made a deal with the devil

…to torture all of earth’s unclaimed souls by forcing them to work with coroutines.

Just spent the last…oh idk…3 hours, trying to simply PAUSE a script for a specific amount of time.

I tried every combination of commands before learning you can’t pause anything in Update. I then tried executing a Coroutine only to learn there’s some specific syntax you need to use that only the Unity developers are aware of. Then I find myself reading about IEnumerators on some obscure website that’s probably giving me spyware.

Finally I gave up and moved everything outside of Update to Start, but that wouldn’t pause the script. Now I’m moving things outside of start and the compiler is giving me syntax errors.

Oh for god sake. Pause. Just pause the script.

Lol… what?

IEnumerator CoroutineThatPauses()
    {
        // stuff...
        // time to pause
        yield return new WaitForSeconds(2);
        // paused... continue stufff
        // move on

    }

Need to call that? 2 is how long it waits for

if(/*condition met here...*/)
{
// might call it more than once if the condtion is met multiple times
StartCoroutine("CoroutineThatPauses");
}

enabled = false; //paused
enabled = true; //not paused

The docs tell you how to use coroutines; it’s not a secret and not hidden. Unity - Manual: Coroutines

–Eric

What do you mean by pause?

For gods sake just learn, learn basic scripting.

bool isPaused = false;
float pauseTime = 0;

void Update(){
   if(isPaused){
     pauseTime += time.deltaTime;
       if(pauseTime > 2){
         isPaused = false;
         pauseTime = 0;
       }
   }
   if(!isPaused){
     //Do stuff here. Say isPaused = true when you dont want this running
   }

}

I always find it best to step away from the computer when it’s giving me a hard time (sometimes that’s easier said then done, however…).

Like aaro4130 said, maybe it’s best to learn the basics of scripting first before diving into deeper concepts. Step back, take a couple days to watch tutorials/practice basic scripting and you may find you won’t get as frustrated in the future with such tasks. For example, I’m not 100% certain, but after programming for 10 years I believe the task you’re attempting to accomplish could be done with a simple ‘if’ condition. Learning basic scripting before diving into Unity projects is the way to remedy this frustration.

Best of luck!

Also I have no clue what kind of websites you are going on to find your information, Microsoft has this stuff documented in full on the MSDN

Ok, I think I’ve got it:

  1. CoRoutines are functions that can be paused.
  2. For a function to qualify as a CoRoutine it needs to be of type “IEnumerator”
  3. You insert “yield” statements into the IEnumerator function to tell unity -where- to pause the program
  4. You specify “yield WaitForSeconds(x)” to pause for x seconds. Default “yield” pauses for 1 frame.
  5. Instead of just typing “myFunction()” to execute a coroutine, you type StartCoroutine(MyFunction)
  6. You need to import System.Collections to identify the IEnumerator type.

As hippo said, Update doesn’t run on inactive components. So you can combine the two approaches and leave your logic in Update.

public class SomeClass : MonoBehaviour
{
    SomeManager someManager;

    void Update()
    {
        DoSomethingGreat();
    }

    void DoSomethingGreat()
    {
        someManager.PauseScript(this);
    }
}
public class SomeManager : MonoBehaviour
{
    public void PauseScript(MonoBehaviour script)
    {
        StartCoroutine(Pause(script));
    }

    IEnumerator Pause(MonoBehaviour script)
    {
        script.enabled = false;
        yield return new WaitForSeconds(2);
        script.enabled = true;
    }
}
1 Like

Hey! It’s lordofduct! Good to see you!

1 Like