WHILE Loop prevents update function from running

Hello!
I am working with an eye tracker in Unity and I am scripting in C#. The script is attached to my camera in the scene.
To detect fixations and saccades I am using a While loop in my Start() function, to catch all the samples I receive from the Eye Tracker. However, when this While loop is running, everything else related to my Unity scene stops being executed! My Update() function will not be executed when I am in this loop. When the loop finishes, it gives the control back to Unity scene.
This is part of my code:

 while (Time.realtimeSinceStartup < 15)
                {
                    elData = el.getNextData();
                    if (elData != null)
                    {
                        switch (elData.eltype)
                        {
                            case SREYELINKLib.EL_DATA_TYPE.EL_SAMPLE_TYPE:
                                file.Write("sample: ");
                                file.Write(elData.time);
                                file.WriteLine("");
                                break;
                            case SREYELINKLib.EL_DATA_TYPE.EL_STARTSACC:
                                stsaccevent = (SREYELINKLib.IStartSaccadeEvent)elData;
                                eevent = (SREYELINKLib.IEyeEvent)elData;
                                if (eevent.eye == eye)
                                {
                                    [COLOR=#00ff00][B]transform.Translate(Vector3.forward * Time.deltaTime * 5f);[/B][/COLOR]
                                    file.Write("start saccade: time= ");
                                    file.Write(elData.time);
                                    file.WriteLine("");
                                }
                                break;
                            case SREYELINKLib.EL_DATA_TYPE.EL_ENDSACC:
                                stsaccevent = (SREYELINKLib.IStartSaccadeEvent)elData;
                                ensaccevent = (SREYELINKLib.IEndSaccadeEvent)elData;
                                eevent = (SREYELINKLib.IEyeEvent)elData;
                                if (eevent.eye == eye)
                                {
                                    file.Write("end saccade:  start time= ");
                                    file.Write(eevent.sttime);
                                    file.Write(" end time= ");
                                    file.Write(ensaccevent.entime);
                                    file.Write("\t");
                                    file.Write("sgx = ");
                                    file.Write(stsaccevent.gstx);
                                    file.Write(" sgy = ");
                                    file.Write(stsaccevent.gsty);
                                    file.Write("\t");
                                    file.Write("endgx = ");
                                    file.Write(ensaccevent.genx);
                                    file.Write(" endgy = ");
                                    file.Write(ensaccevent.geny);
                                    file.WriteLine("");
                                }
                                break;
                            case SREYELINKLib.EL_DATA_TYPE.EL_STARTFIX:
                                stfixevent = (SREYELINKLib.IStartFixationEvent)elData;
                                eevent = (SREYELINKLib.IEyeEvent)elData;
                                if (eevent.eye == eye)
                                {
                                    file.Write("start fixation: time= ");
                                    file.Write(elData.time);
                                    file.WriteLine("");
                                }
                                break;
                        }
                    }
               }
     

    // Update is called once per frame
    void Update () {
        timer = Time.realtimeSinceStartup;
        if (timer > 1 && timer < 20)
            transform.Translate(Vector3.forward * Time.deltaTime); //* 1.2f);
        if (timer > 21 && timer < 30)
            transform.Rotate(0, -0.1f, 0);
    }

When the while loop is running, even the line I have highlighted in green would not execute.

I would appreciate any help or suggestions.

If you have a while loop that is not a coroutine, the entire game freezes until it finishes. All the main game code in Unity is single-threaded, which means that your while loop is preventing everything from executing until it’s done - not just Update, but also rendering, etc. You’ve got a single 15-second-long frame there.

Your two best options are: You can either make your while loop a coroutine - where it “yields” once in your while loop, and lets the rest of the engine run for a frame before picking up where it left off - or you can put your loop in another thread, which will be able to run in parallel. Depending on your level of code expertise outside Unity, threading can be complicated to work out, while coroutines are kinda Unity-specific and are simpler to understand, and probably make more sense too.

2 Likes

Thank you StarManta for this information! I got it sorted with Coroutines. Thanks

1 Like