Here is the relevant code:
private void Awake()
{
StartCoroutine(LoadScenes());
}
private IEnumerator LoadScenes()
{
this.sceneLoad1 = SceneManager.LoadSceneAsync(Scene1, LoadSceneMode.Additive);
while (!this.sceneLoad.isDone)
{
Debug.Log("Loading scene 1...");
yield return null;
}
this.sceneLoad2 = SceneManager.LoadSceneAsync(Scene2, LoadSceneMode.Additive);
while (!this.sceneLoad.isDone)
{
Debug.Log("Loading scene 2...");
yield return null;
}
}
private void Start()
{
SceneManager.GetSceneByName(Scene1)
.GetRootGameObjects()
.Where(root => root.GetComponent<God>() != null)
.Select(root => root.GetComponent<God>())
.Single()
.SpreadGospel();
}
(Scene1 and Scene2 are const strings.)
The program works fine when running in the Unity editor. However, when I make a development build, I see the follow error message:
InvalidOperationException: Operation is not valid due to the current state of the object
at System.Linq.Enumerable.Single[God] (IEnumerable`1 source) [0x00057] in /Users/builduser/buildslave/mono/build/mcs/class/System.Core/System.Linq/Enumerable.cs:1968
at MySolution.GodInitializer.SpreadGospel () [0x00001] in D:\repos\MySolution\SourceCode\SourceCode\GodInitializer.cs:100
at MySolution.GodInitializer.Start () [0x00012] in D:\repos\MySolution\SourceCode\SourceCode\GodInitializer.cs:77
... which indicates that the scene wasn't fully loaded before I tried to access the God singleton.
What changes should I make to fix the problem?
FindObjectOfType<God>- or you could just haveGoditself provide aGetInstance()getter populated in its ownAwake. \$\endgroup\$