-
同学你好,可以参考一下话题区的解决方法
NullReferenceException: Object reference not set to an instance of an objectNo21_SceneManager.Update...
NullReferenceException: Object reference not set to an instance of an object
No21_SceneManager.Update () (at Assets/Scripts/No21_SceneManager.cs:27)
最后一节场景管理一直会报空指针异常,有人知道吗?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
//功能说明:场景管理
public class No21_SceneManager : MonoBehaviour
{
private AsyncOperation ao;
void Start()
{
//SceneManager.LoadScene(1);
//SceneManager.LoadScene("TriggerTest");
//SceneManager.LoadScene(2);
//SceneManager.LoadSceneAsync(2);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
//SceneManager.LoadSceneAsync(2);
StartCoroutine(LoadNextAsyncScene());
}
if (Input.anyKeyDown && ao.progress >= 0.9f)
{
ao.allowSceneActivation = true;
}
}
IEnumerator LoadNextAsyncScene()
{
ao = SceneManager.LoadSceneAsync(1);
ao.allowSceneActivation = false;
while (ao.progress < 0.9f)
{
//当前场景加载进度小于0.9
//当前场景挂起,一直加载,直到加载基本完成
yield return null;
}
Debug.Log("按下任意键继续游戏");
}
}
No21_SceneManager.Update () (at Assets/Scripts/No21_SceneManager.cs:27)
最后一节场景管理一直会报空指针异常,有人知道吗?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
//功能说明:场景管理
public class No21_SceneManager : MonoBehaviour
{
private AsyncOperation ao;
void Start()
{
//SceneManager.LoadScene(1);
//SceneManager.LoadScene("TriggerTest");
//SceneManager.LoadScene(2);
//SceneManager.LoadSceneAsync(2);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
//SceneManager.LoadSceneAsync(2);
StartCoroutine(LoadNextAsyncScene());
}
if (Input.anyKeyDown && ao.progress >= 0.9f)
{
ao.allowSceneActivation = true;
}
}
IEnumerator LoadNextAsyncScene()
{
ao = SceneManager.LoadSceneAsync(1);
ao.allowSceneActivation = false;
while (ao.progress < 0.9f)
{
//当前场景加载进度小于0.9
//当前场景挂起,一直加载,直到加载基本完成
yield return null;
}
Debug.Log("按下任意键继续游戏");
}
}