- Home /
The question is answered, right answer was accepted
Can't access AsyncOperation instance to change allowSceneActivation
I'm trying to load a scene in the background, initially setting allowSceneActivation to false, then setting it to true once a button is placed. When I try to set it to true, though, Unity gives me an error, saying 'Object reference not set to an instance of an object'. the variable in question is, indeed set to an instance, though, in the Start function:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameLauncher : MonoBehaviour {
AsyncOperation async;
// Use this for initialization
void Start () {
//load other scene in background
StartCoroutine(loadScene());
//connect to server in background
}
public IEnumerator loadScene()
{
async = SceneManager.LoadSceneAsync(1, LoadSceneMode.Single);
async.allowSceneActivation = false;
yield return null;
}
here's where I get the error:
public void Connect () {
//finish connection when button is pressed
async.allowSceneActivation = true;
}
I don't get why thIs assignment in the loadScene coroutine doesn't count? Is the Connect function being public somehow responsible? It's hard to troubleshoot, since Unity crashes every time I fail to finish the scene load...
Answer by asdfadsfgadfg · Jul 21, 2017 at 10:32 PM
The problem as stated isn't reproducible, it was a misunderstanding.
Answer by _watcher_ · Jul 15, 2017 at 03:38 PM
Just a guess - try using LoadSceneMode.Additive ? Your script and further execution is removed as soon ad the new scene using LoadSceneMode.Single is loaded (because it resides in your previous scene).
thanks for your reply! And sorry it took me so long to respond. The problem turned out to be much dumber than I thought; there were two buttons in the scene, and the one which had been linked properly was disabled. So allowSceneActivation was never being set to true after all.