Unity - Issue when switching scenes

I have a problem when switching back and forth between two scenes.
I have a lobby and a game scene. In the game scene i use the code you provide to create the engine.
Each time i switch to the lobby scene and then back to the game scene the scene loading takes a bit longer and the monitor extends to left outside the bounds of the screen.

To me it looks like the engine is not getting destroyed when switching scenes. Could this be the case?

Assuming the switching context is all happening in the editor, and you are creating an engine in your Start method, you may need to add this code so you only instantiate a Pulse engine when your application is playing when context switching

void Start()
{
    // Ensure we only read data if the application is playing
    // and we have a state file to initialize the engine with
    if (!Application.isPlaying || initialStateFile == null)
        return;

  // Now create a Pulse engine
  engine = new PulseEngine(logFilePath, pulseDataPath);

You could also put a break point on the engine = new PulseEngine(logFilePath, pulseDataPath); call when you context switch in the editor to see if you are indeed creating an engine on a scene switch

Thanks for the reply. Maybe I did not express myself correctly. It does not occur when switching scenes in Editor by clicking a scene file. I occurs when i switch between scenes during runtime by using SceneManager.LoadScene.
I do create a new engine in the Start method. I expected the engine to get destroyed when loading another scene, but it seems like that is not the case.

I use the provided code each time i load the scene:

protected virtual void Start()
  {
    // Ensure we only read data if the application is playing
    // and we have a state file to initialize the engine with
    if (!Application.isPlaying)
      return;

    // Allocate PulseEngine with path to logs and needed data files
    string dateAndTimeVar = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
    string logFilePath = Application.persistentDataPath + "/" +
                                    gameObject.name +
                                    dateAndTimeVar + ".log";
    engine = new PulseEngine();
    //engine.SetLogFilename(logFilePath);

    SEDataRequestManager data_mgr = new SEDataRequestManager(data_requests);

    // NOTE, there are other ways to initialize the engine, see here
    // https://gitlab.kitware.com/physiology/engine/-/blob/3.x/src/csharp/howto/HowTo_EngineUse.cs

    // Initialize engine state from tje state file content
    if (initialStateFile != null)
    {
      if (!engine.SerializeFromString(initialStateFile.text, data_mgr, serializationFormat))
        Debug.unityLogger.LogError("PulsePhysiologyEngine", "Unable to load state file " + initialStateFile);
    }
    else
    {
      // You do not have to use the Editor control if you don't want to,
      // You could simply specify a file on disk via use of the Streaming Assets folder
      string state = Application.streamingAssetsPath + "/Data/states/StandardMale@0s.pbb";
      if (!engine.SerializeFromFile(state, data_mgr))
        Debug.unityLogger.LogError("PulsePhysiologyEngine", "Unable to load state file " + state);
    }

    pulseTime = 0;
    pulseSampleTime = 0;
    }

The image shows the display after switching scenes several times. It keeps extending each time i load the scene instead of resetting

Is there a way to manually destroy / reset the engine?

It seems like your lineRenderer object used in the vitals monitor is not properly checking it’s length
https://gitlab.kitware.com/physiology/unity/-/blob/master/Scripts/PulseDataLineRenderer.cs#L152
And removing points when it gets to the edge of the monitor

Or maybe the lineRenderer is consuming and displaying data from multiple engines?
I am really not sure without looking at your whole project code base and how things are constructed.

So, you are using C#, so you don’t really have control of when objects get deconstructed
Generally, once the reference count = 0 the automatic garbage collector deconstructs the object
You can decrement your reference count by setting your engine pointer to null

void OnApplicationQuit()
    {
        engine = null;
    }

It seems like maybe this is due to how your application is architected

If you can somehow reproduce this behavior within the provided Pulse asset, I can try to debug it

You can also just reuse the engine instance
You can call engine.SerializeFromString on the same engine object
You do not have to allocate a new engine each time in Start()
You can just allocate the engine in the class constructor

Maybe this might help?

I am was able to reproduce the issue with the unity asset you provide on the store.
Unity Version used: 2020.3.31f1
Engine Version 3.0

Create a new empty scene.
In the empty scene attach a script in which you load the “VitalsMonitor” scene in the start method.

In the “VitalsMonitor” scene create a script and load the new empty scene on button / key press.

Now when you enter play mode in the “VitalsMonitor” scene and switch to the empty scene and back several times the line renderer keeps extending.

Can you zip up that project and provide it to me?
Dropbox, Drive, email?

I can send you a WeTransfer link with the zip file.
What mail address do you want me to send the file to?

aaron.bray@kitware.com

Got it
I will take a look and see what I can see
(Note my unity skills are not that great)

Hey @Desard

I took a look at this
I am running 2020.3.33f1
I just opened and ran the project
I ran the Test scene and it seems to behave pretty well

What exactly in the editor are you doing again?