Skip to content
This repository was archived by the owner on May 9, 2025. It is now read-only.

Fix SceneIndex different from index in build settings which can cause exceptions when using LoadScene(int) #156

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Assets/SO Architecture/Variables/SceneVariable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,21 @@ public void OnBeforeSerialize()
var scenes = UnityEditor.EditorBuildSettings.scenes;

SceneIndex = -1;
int enabledSceneIndex = 0;//scenes are only given a build index if enabled.
for (var i = 0; i < scenes.Length; i++)
{
bool sceneIsEnabled = scenes[i].enabled;
if (scenes[i].guid.ToString() == sceneAssetGUID)
{
SceneIndex = i;
IsSceneEnabled = scenes[i].enabled;
if(sceneIsEnabled)
SceneIndex = enabledSceneIndex++;
IsSceneEnabled = sceneIsEnabled;
break;
}
else if (sceneIsEnabled)
{
++enabledSceneIndex;
}
}
}
#endif
Expand Down