Skip to content

Commit e24423d

Browse files
committed
fix: add warning and documentation regarding Awake() (#43)
1 parent 9855e6b commit e24423d

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

Packages/com.mygamedevtools.scene-loader/Runtime/AdvancedSceneManager.cs

+5
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,15 @@ public AdvancedSceneManager(bool addLoadedScenes)
5656
_loadedScenes.Add(SceneDataBuilder.BuildFromScene(scene));
5757
}
5858
}
59+
5960
if (loadedSceneCount > 0 && SceneDataUtilities.TryGetSceneDataByLoadedScene(SceneManager.GetActiveScene(), _loadedScenes, out ISceneData sceneData))
6061
{
6162
_activeScene = sceneData;
6263
}
64+
else if (loadedSceneCount == 0)
65+
{
66+
Debug.LogWarning("Tried to create an `AdvancedSceneManager` with all loaded scenes, but encoutered none. Did you create the scene manager on `Awake()`? If so, try moving the call to `Start()` instead.");
67+
}
6368
}
6469
/// <summary>
6570
/// Creates a new <see cref="AdvancedSceneManager"/> with the option to add a list of loaded scenes to its management.

README.md

+58-3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ Refer to the [Migration Guide](https://github.com/mygamedevtools/scene-loader/wi
7171
* [Loading Screen Example](#loading-screen-example)
7272
* [Why so many interfaces?](#why-so-many-interfaces)
7373
* [Tests](#tests)
74+
* [Troubleshooting](#troubleshooting)
75+
* [Error when creating an AdvancedSceneManager](#error-when-creating-an-advancedscenemanager)
76+
* [Cannot unload a scene with a different ILoadSceneInfo](#cannot-unload-a-scene-with-a-different-iloadsceneinfo)
7477

7578
## Installation
7679

@@ -208,6 +211,7 @@ You can create an `AdvancedSceneManager` using three constructors:
208211

209212
```cs
210213
// Creates an advanced scene manager including all currently loaded scenes. Useful for most cases.
214+
// Should not be called on `Awake()`, since it runs before the scene is loaded.
211215
new AdvancedSceneManager(addLoadedScenes: true);
212216

213217
// Creates an empty advanced scene manager. Useful if you are doing this before any scene loads or in a bootstrap scene.
@@ -523,9 +527,9 @@ sceneLoader.UnloadScenes(sceneInfoGroup);
523527
sceneLoader.TransitionToScenes(sceneInfoGroup, 0);
524528

525529
// Awaitable alternatives
526-
await sceneLoader.LoadScenes(sceneInfoGroup);
527-
await sceneLoader.UnloadScenes(sceneInfoGroup);
528-
await sceneLoader.TransitionToScenes(sceneInfoGroup, 0);
530+
await sceneLoader.LoadScenesAsync(sceneInfoGroup);
531+
await sceneLoader.UnloadScenesAsync(sceneInfoGroup);
532+
await sceneLoader.TransitionToScenesAsync(sceneInfoGroup, 0);
529533
```
530534

531535
[_[back to top]_](#advanced-scene-manager)
@@ -635,6 +639,57 @@ The tests do not have any effect on a runtime build of the game, they only mean
635639

636640
[_[back to top]_](#advanced-scene-manager)
637641

642+
## Troubleshooting
643+
644+
### Error when creating an `AdvancedSceneManager`
645+
646+
When creating an `AdvancedSceneManager` passing a `true` value to its constructor, as `new AdvancedSceneManager(true)`, it attempts to add all loaded scenes to its list of tracked scenes.
647+
However, if you called that during `Awake()`, you might see the error:
648+
649+
```
650+
ArgumentException: Attempted to get an {nameof(ISceneData)} through an invalid or unloaded scene.
651+
```
652+
653+
This error is thrown because during `Awake()` the scene is not fully loaded and cannot be added to the list of tracked scenes.
654+
655+
656+
Move your call to `Start()` instead.
657+
658+
### Cannot unload a scene with a different `ILoadSceneInfo`
659+
660+
In a case where you have loaded a scene via one type of `ILoadSceneInfo`, you can only unload it by using the same type or explicitly a `LoadSceneInfoScene`. For example:
661+
662+
```cs
663+
ILoadSceneInfo nameInfo = new LoadSceneInfoName("MyScene");
664+
ILoadSceneInfo indexInfo = new LoadSceneInfoIndex(3);
665+
666+
sceneManager.LoadSceneAsync(nameInfo);
667+
668+
// You **cannot** do this:
669+
sceneManager.UnloadSceneAsync(indexInfo);
670+
671+
// But you can do this:
672+
sceneManager.UnoadSceneAsync(nameInfo);
673+
674+
// Or, build a `LoadSceneInfoScene`.
675+
// Alternatives: GetLoadedSceneByName(name), GetLoadedSceneAt(index), GetLastLoadedScene() or GetActiveScene()
676+
ILoadSceneInfo sceneInfo = sceneManager.GetLoadedSceneByName("MyScene");
677+
sceneManager.UnloadSceneAsync(sceneInfo);
678+
```
679+
680+
Sometimes this issue can also be avoided by performing a scene transition. If you're trying to unload the active scene to transition between scenes, you can execute the transition through the scene manager and let it handle the internal complexity. For example:
681+
682+
```cs
683+
// Instead of unloading the source scene directly:
684+
sceneManager.LoadSceneAsync(targetSceneInfo)
685+
sceneManager.UnloadSceneAsync(sourceSceneInfo);
686+
687+
// Perform a scene transition:
688+
sceneManager.TransitionToScene(targetSceneInfo);
689+
```
690+
691+
[_[back to top]_](#advanced-scene-manager)
692+
638693
---
639694

640695
Don't hesitate to create [issues](https://github.com/mygamedevtools/scene-loader/issues) for suggestions and bugs. Have fun!

0 commit comments

Comments
 (0)