# Two engines, how to return to the first, after interacting with the second?

**URL:** <https://forum.babylonjs.com/t/two-engines-how-to-return-to-the-first-after-interacting-with-the-second/55951>\
**Category:** Questions\
**Created:** [January 14, 2025, 3:21pm UTC](https://forum.babylonjs.com/t/two-engines-how-to-return-to-the-first-after-interacting-with-the-second/55951 "2025-01-14T15:21:22Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![D\_Taylor](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/d_taylor/32/27277_2.png) [@D\_Taylor](https://forum.babylonjs.com/u/D_Taylor)\
**Post date:** [January 14, 2025, 3:21pm UTC](https://forum.babylonjs.com/t/two-engines-how-to-return-to-the-first-after-interacting-with-the-second/55951/1 "2025-01-14T15:21:22Z")

</div>

When loading the page, I create the first engine and scene.  
Then I create the second engine and the scene in it in a pop-up window. After closing the window, as I understand it, Babylon JS is in the second engine, and when interacting with the first scene, errors appear. How to return to the first engine?  
I hope I was able to explain, I don’t know how to make a playground  
Thanks for your help!

---

<div class="post-metadata">

**Author:** ![D\_Taylor](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/d_taylor/32/27277_2.png) [@D\_Taylor](https://forum.babylonjs.com/u/D_Taylor)\
**Post date:** [January 14, 2025, 3:55pm UTC](https://forum.babylonjs.com/t/two-engines-how-to-return-to-the-first-after-interacting-with-the-second/55951/2 "2025-01-14T15:55:18Z")

</div>

Solved the problem with this  
BABYLON.EngineStore.\_LastCreatedScene = scene1  
Is that correct or is there another solution?

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [January 14, 2025, 4:27pm UTC](https://forum.babylonjs.com/t/two-engines-how-to-return-to-the-first-after-interacting-with-the-second/55951/3 "2025-01-14T16:27:03Z")

</div>

I’m not sure to get your problem but if this works for you I’m fine. That being said, this may mean you are calling new XXX without giving the current scene (this is why by default Babylon will take the latest created one)

Also latest created should be automatically updated when you dispose your secondary scene and engine so make sure to call dispose on these objects

---

<div class="post-metadata">

**Author:** ![D\_Taylor](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/d_taylor/32/27277_2.png) [@D\_Taylor](https://forum.babylonjs.com/u/D_Taylor)\
**Post date:** [January 14, 2025, 4:33pm UTC](https://forum.babylonjs.com/t/two-engines-how-to-return-to-the-first-after-interacting-with-the-second/55951/4 "2025-01-14T16:33:59Z")

</div>

Yes, I did scene2.dispose(); engine2.dispose();  
But after that BABYLON.EngineStore.LastCreatedScene returned null

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [January 14, 2025, 4:58pm UTC](https://forum.babylonjs.com/t/two-engines-how-to-return-to-the-first-after-interacting-with-the-second/55951/5 "2025-01-14T16:58:04Z")

</div>

yeah I see that  
totally a bug! let me fix that 🙂

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [January 14, 2025, 5:04pm UTC](https://forum.babylonjs.com/t/two-engines-how-to-return-to-the-first-after-interacting-with-the-second/55951/6 "2025-01-14T17:04:50Z")

</div>

[Fix scene dispose to restore LastCreatedScene correctly by deltakosh · Pull Request #16067 · BabylonJS/Babylon.js](https://github.com/BabylonJS/Babylon.js/pull/16067)

---

<div class="post-metadata">

**Author:** ![D\_Taylor](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/d_taylor/32/27277_2.png) [@D\_Taylor](https://forum.babylonjs.com/u/D_Taylor)\
**Post date:** [January 15, 2025, 11:38am UTC](https://forum.babylonjs.com/t/two-engines-how-to-return-to-the-first-after-interacting-with-the-second/55951/7 "2025-01-15T11:38:50Z")

</div>

Thank you!  
Another question, how to switch between simultaneously running engines?  
Is this ok?  
BABYLON.EngineStore.\_LastCreatedScene = scene1 //active engine 1  
else  
BABYLON.EngineStore.\_LastCreatedScene = scene2 //active engine 2

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [January 15, 2025, 4:00pm UTC](https://forum.babylonjs.com/t/two-engines-how-to-return-to-the-first-after-interacting-with-the-second/55951/8 "2025-01-15T16:00:52Z")

</div>

you should never do that 🙂  
The lastCreatedScene is only for people who forgets to set the scene parameter when they create an object

If you need to rely on it, it means you are creating mesh, camera, lights or materials without setting the scene parameter and this is bad. Somewhere a kitten may die because of that

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [January 16, 2025, 2:01am UTC](https://forum.babylonjs.com/t/two-engines-how-to-return-to-the-first-after-interacting-with-the-second/55951/9 "2025-01-16T02:01:11Z")

</div>

This escalated quickly lol I was definitely not ready for it

---

<div class="post-metadata">

**Author:** ![D\_Taylor](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/d_taylor/32/27277_2.png) [@D\_Taylor](https://forum.babylonjs.com/u/D_Taylor)\
**Post date:** [January 16, 2025, 9:09am UTC](https://forum.babylonjs.com/t/two-engines-how-to-return-to-the-first-after-interacting-with-the-second/55951/10 "2025-01-16T09:09:40Z")

</div>

Yes, I understand. But what if two scenes are running in  
two canvases at the same time?

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [January 16, 2025, 4:32pm UTC](https://forum.babylonjs.com/t/two-engines-how-to-return-to-the-first-after-interacting-with-the-second/55951/11 "2025-01-16T16:32:36Z")

</div>

then they are completely separated (not the same engine) so they should never get mixed up
