使用StartCoroutine()的NullReferenceException

时间:2016-06-29 10:42:49

标签: c# unity3d visual-studio-2015 nullreferenceexception

我制作了会在游戏中加载的协程,但每次点击播放按钮我都会在线上获得NullReferenceException:StartCoroutine(LoadNewScene());

这是我的完整代码:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Loading : MonoBehaviour
{

    private bool LoadScene = false;

    [SerializeField]
    private int scene = 0;
    [SerializeField]
    private Text LoadingText;
    [SerializeField]
    private Text TipText;

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (LoadScene == true)
        {
            scene++;
        }
    }

    public void Load()
    {
        LoadScene = true;
        //LoadingText.color = new Color(LoadingText.color.r, LoadingText.color.g, LoadingText.color.b, Mathf.PingPong(Time.time, 1));
        StartCoroutine(LoadNewScene()); //here is my error
    }

    IEnumerator LoadNewScene() // my coroutine
    {
        yield return new WaitForSeconds(3);

        AsyncOperation async = SceneManager.LoadSceneAsync(scene);

        while (!async.isDone)
        {
            yield return null;
        }
    }
}

0 个答案:

没有答案