0

I have a function in my script that calls 5 commands, like this:

public class ScrambleScript : MonoBehaviour {    
    public RRotation ScrambleR; // reference for R rotation 
    public LRotation ScrambleL; // reference for L rotation
    public FRotation ScrambleF; // reference for F rotation
    public DRotation ScrambleD; // reference for D rotation
    public Rotation  ScrambleU; // reference for U rotation

    void ScrambleCube() {    
        ScrambleR.ScrambleRNotation();
        ScrambleU.ScrambleUNotation();
        ScrambleL.ScrambleLNotation();
        ScrambleF.ScrambleFNotation();
        ScrambleD.ScrambleDNotation();
    }
}

Once I call this function in the Update(), the commands will be called every frame, however I want to be able to make another delay function that I can use to have a 1 second pause in between each function call, so it would look something like this:

void ScrambleCube() {    
        ScrambleR.ScrambleRNotation();
        // waits 1 second here.
        ScrambleU.ScrambleUNotation();
        // waits 1 second here.
        ScrambleL.ScrambleLNotation();
        // waits 1 second here.
        ScrambleF.ScrambleFNotation();
        // waits 1 second here.
        ScrambleD.ScrambleDNotation();
    }

I have tried using the IEnumerator function but I can't seem to get that to work if I paste the delay in between each function call as I've shown above. Is there another way to do this? Or was I using the IEnumerator incorrectly?

Galandil
  • 4,169
  • 1
  • 13
  • 24

1 Answers1

1

You can use a principle in Unity called Coroutines, using IEnumerators.

public IEnumerator ScrambleCube () {
    ScrambleR.ScrambleRNotation();
    yield return new WaitForSeconds(1); // This pauses the execution of 
                                        // the function for 1 second
    ScrambleU.ScrambleUNotation();
    // etc...
}

Note that you can't call coroutines simply by their method name, you have to use one of MonoBehaviours methods called StartCoroutine.

StartCoroutine(ScrambleCube());

Note that starting the coroutine will not delay the execution of the method that issued the StartCoroutine call.

Ian H.
  • 3,840
  • 4
  • 30
  • 60