1
\$\begingroup\$

I am trying to display instructions within the Unity game. The idea is to have the instructions appear over time. For example, "instruction 1" then wait 5 seconds and show "instruction 2" instead of instruction 1 (on the same spot, in this case, I am changing the "message" variable in the code to change the instructions). This code does change the instructions but the problem is that it does it in a haphazard fashion. It appears that "instruction 1" is replaced with "instruction 2" and then it reverts back to "instruction 1" within a second and then back to "instruction 2" so instead of just 1-appearing->1-disappearing->2-appearing->etc in a smooth fashion, it does this weird flashing thing before proceeding to the next instruction. How could I fix this?

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Growing : MonoBehaviour {

    public Text instructions;
    public string message;

    void Start () { 
         instructions.text = message;
    }

    void Update () {

         Instructions();

         //other code to begin moving a game object
    }

    IEnumerator Instructions() {
        //run instructions
        message = "GET READY FOR YOUR FOCUSED BREATHING EXERCISE";
        yield return new WaitForSecondsRealtime(5);
        message = "MAKE SURE YOUR BODY IS COMFORTABLE";
        yield return new WaitForSecondsRealtime(5);
    }
\$\endgroup\$
1
\$\begingroup\$

Or the coroutine method:

You were almost correct, what you needed to do was wrap the Instructions() invocation inside a StartCoroutine() call. This lets Unity add the method to the task scheduler. Then, make sure you call it only once, either by putting it in Start() (as I've done below) or inside some other conditional that prevents multiple copies of the coroutine from being created simultaneously.

void Start () {

     StartCoroutine(Instructions());

     //other code to begin moving a game object
}

IEnumerator Instructions() {
    //run instructions
    message = "GET READY FOR YOUR FOCUSED BREATHING EXERCISE";
    yield return new WaitForSecondsRealtime(5);
    message = "MAKE SURE YOUR BODY IS COMFORTABLE";
    yield return new WaitForSecondsRealtime(5);
}
\$\endgroup\$
0
\$\begingroup\$

You start a new coroutine every time you call "update". YOu are not the first to ask this, and I'm not the first to answer, so let me just quote someone else:

Yes, coroutines run independently, like separate threads. When you start a coroutine, an instance of it is created and starts running. If you start the same coroutine again, another instance is created and runs independently of the other, and so on. This is a blessing in some special cases, but much more often may become a curse: if you start a coroutine each Update, for example (a very common error), a new instance will be created each frame, crowding the memory and eating the CPU time in a few minutes

http://answers.unity3d.com/questions/309613/calling-startcoroutine-multiple-times-seems-to-sta.html

As a starting point, try the following instead:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Growing : MonoBehaviour {

    public Text instructions;
    float instructionStartTime = -1;

    void Start () { 
        LaunchInstructions();
    }

    void LaunchInstructions()
    {
        instructionStartTime = Time.time;
    }

    void Update () {
        if (instructionStartTime > 0 && Time.time >= instructionStartTime)
        {
            if (Time.time - instructionStartTime < 5)
                instructions.text = "GET READY FOR YOUR FOCUSED BREATHING EXERCISE";
            else if (Time.time - instructionStartTime < 10)
                instructions.text = "MAKE SURE YOUR BODY IS COMFORTABLE";
            else
            {
                instructionStartTime = -1;
                instructions.text = "";
            }
        }
        //other code to begin moving a game object
    }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.