# Unity - Issue when switching scenes

**URL:** https://discourse.kitware.com/t/unity-issue-when-switching-scenes/810
**Category:** Pulse Physiology Engine
**Created:** [March 14, 2022, 2:30pm UTC](https://discourse.kitware.com/t/unity-issue-when-switching-scenes/810 "2022-03-14T14:30:00Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Desard](https://discourse.kitware.com/letter_avatar_proxy/v4/letter/d/eb9ed0/32.png) [@Desard](https://discourse.kitware.com/u/Desard)
#### Post date: [March 14, 2022, 2:30pm UTC](https://discourse.kitware.com/t/unity-issue-when-switching-scenes/810/1 "2022-03-14T14:30:00Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![abray](https://discourse.kitware.com/user_avatar/discourse.kitware.com/abray/32/12_2.png) [@abray](https://discourse.kitware.com/u/abray)
#### Post date: [March 14, 2022, 2:56pm UTC](https://discourse.kitware.com/t/unity-issue-when-switching-scenes/810/2 "2022-03-14T14:56:04Z")

</div>

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

```auto
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

---

<div class="post-metadata">

### Author: ![Desard](https://discourse.kitware.com/letter_avatar_proxy/v4/letter/d/eb9ed0/32.png) [@Desard](https://discourse.kitware.com/u/Desard)
#### Post date: [March 14, 2022, 4:36pm UTC](https://discourse.kitware.com/t/unity-issue-when-switching-scenes/810/3 "2022-03-14T16:36:08Z")

</div>

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:

```auto
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;
    }

```

 ![11](https://discourse.kitware.com/uploads/default/original/1X/ff1a2656b4942ecd7b8801db1cd14019a6343817.jpeg)

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?

---

<div class="post-metadata">

### Author: ![abray](https://discourse.kitware.com/user_avatar/discourse.kitware.com/abray/32/12_2.png) [@abray](https://discourse.kitware.com/u/abray)
#### Post date: [March 14, 2022, 5:16pm UTC](https://discourse.kitware.com/t/unity-issue-when-switching-scenes/810/4 "2022-03-14T17:16:24Z")

</div>

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](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

```auto
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

---

<div class="post-metadata">

### Author: ![abray](https://discourse.kitware.com/user_avatar/discourse.kitware.com/abray/32/12_2.png) [@abray](https://discourse.kitware.com/u/abray)
#### Post date: [March 14, 2022, 5:23pm UTC](https://discourse.kitware.com/t/unity-issue-when-switching-scenes/810/5 "2022-03-14T17:23:00Z")

</div>

Maybe this might help?

> **[Unity - Scripting API: Object.DontDestroyOnLoad](https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html)**

---

<div class="post-metadata">

### Author: ![Desard](https://discourse.kitware.com/letter_avatar_proxy/v4/letter/d/eb9ed0/32.png) [@Desard](https://discourse.kitware.com/u/Desard)
#### Post date: [March 25, 2022, 12:07pm UTC](https://discourse.kitware.com/t/unity-issue-when-switching-scenes/810/6 "2022-03-25T12:07:34Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![abray](https://discourse.kitware.com/user_avatar/discourse.kitware.com/abray/32/12_2.png) [@abray](https://discourse.kitware.com/u/abray)
#### Post date: [March 25, 2022, 12:09pm UTC](https://discourse.kitware.com/t/unity-issue-when-switching-scenes/810/7 "2022-03-25T12:09:34Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Desard](https://discourse.kitware.com/letter_avatar_proxy/v4/letter/d/eb9ed0/32.png) [@Desard](https://discourse.kitware.com/u/Desard)
#### Post date: [March 25, 2022, 12:22pm UTC](https://discourse.kitware.com/t/unity-issue-when-switching-scenes/810/8 "2022-03-25T12:22:10Z")

</div>

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

---

<div class="post-metadata">

### Author: ![abray](https://discourse.kitware.com/user_avatar/discourse.kitware.com/abray/32/12_2.png) [@abray](https://discourse.kitware.com/u/abray)
#### Post date: [March 25, 2022, 12:22pm UTC](https://discourse.kitware.com/t/unity-issue-when-switching-scenes/810/9 "2022-03-25T12:22:36Z")

</div>

[aaron.bray@kitware.com](mailto:aaron.bray@kitware.com)

---

<div class="post-metadata">

### Author: ![abray](https://discourse.kitware.com/user_avatar/discourse.kitware.com/abray/32/12_2.png) [@abray](https://discourse.kitware.com/u/abray)
#### Post date: [March 25, 2022, 12:29pm UTC](https://discourse.kitware.com/t/unity-issue-when-switching-scenes/810/11 "2022-03-25T12:29:29Z")

</div>

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

---

<div class="post-metadata">

### Author: ![abray](https://discourse.kitware.com/user_avatar/discourse.kitware.com/abray/32/12_2.png) [@abray](https://discourse.kitware.com/u/abray)
#### Post date: [April 19, 2022, 2:07pm UTC](https://discourse.kitware.com/t/unity-issue-when-switching-scenes/810/12 "2022-04-19T14:07:12Z")

</div>

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?
