0

我是 Unity 和 C 的新手,这个问题让我发疯,我觉得我错过了明显的,

基本上我有一个游戏对象,我需要向左或向右滑动(GearVR)旋转

当场景最初加载时,一切都很好。当我加载一个新场景然后重新加载主菜单场景时,启动协程会导致 NullReferenceException 错误。

整个事情在编辑器中完美运行,只有在 Android 设备上我才会收到错误消息。

我已经阅读了我能找到的所有内容,但我不明白 StartCoroutine 是如何引发错误的。

请帮忙

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



    public class ApplicationManager : MonoBehaviour {

    public OVRScreenFade leftEye;
    public OVRScreenFade rightEye;
    public OVRScreenFade centerEye;

    public float mSplashDuration = 1.8f;
    private float mFadeToBlackDuration = 1.0f;
    private bool mTransitionStarted = false;
    private bool mShouldFade = false;
    private int mScene = 1;
    private GameObject mMenu;

    public float DegreesPerSecond = 180f; // degrees per second
    private Vector3 currentRot, targetRot;
    private bool rotating = false;


    void Start() {
        Debug.Log("AM START");
        OVRTouchpad.Create();
        OVRTouchpad.TouchHandler += HandleTouchHandler;
        mMenu = GameObject.Find("Menu");

    }
    void Awake()
    {
        mMenu = GameObject.Find("Menu");
    }

        void Update () {
        detectInputEvents();
        // Check if there were any objects hit by our reticle-ray cast in the scene. If so, check whether or not
        // it has a TextureCycler component.
        if (Raycaster.getInstance().anythingHitByRay()) {

            GameObject objHitByRay = Raycaster.getInstance().getObjectHitByRay();
            string objHitTag = objHitByRay.tag;

            // Check that there was a valid object hit by the raycast. Raycaster.getInstance().getObjectHitByRay() 

            if(objHitByRay != null) {
                if(objHitTag == "Button1")
                {
                    Invoke("launchNextScene", mSplashDuration);
                    mShouldFade = true;
                    mScene = 2;
                    //Application.LoadLevel(2);

                }
                if (objHitTag == "Button2")
                {
                    Invoke("launchNextScene", mSplashDuration);
                    mShouldFade = true;
                    mScene = 3;
                    //Application.LoadLevel(3);

                }

                if (!mTransitionStarted && mShouldFade && Time.time >= timeFadeShouldStart())
                {
                    leftEye.StartFadeOut();
                    rightEye.StartFadeOut();
                    centerEye.StartFadeOut();
                    mTransitionStarted = true;
                }
            }
        }


    }


    void HandleTouchHandler(object sender, System.EventArgs e)
    {

        OVRTouchpad.TouchArgs touchArgs = (OVRTouchpad.TouchArgs)e;
        if (touchArgs.TouchType == OVRTouchpad.TouchEvent.Left)
        {
            Debug.Log("Swipe Left");

            StartCoroutine(RotateLeft());


        }

        if (touchArgs.TouchType == OVRTouchpad.TouchEvent.Right)
        {
            Debug.Log("Swipe Right");

            StartCoroutine(RotateRight());


        }

        if (touchArgs.TouchType == OVRTouchpad.TouchEvent.SingleTap)
        {
            Raycaster.getInstance().checkHit();
        }
    }

    void detectInputEvents()
    {

        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            Debug.Log("Left");

            StartCoroutine(RotateLeft());
        }
        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
             Debug.Log("Right");

            StartCoroutine(RotateRight());

        }
    }

    IEnumerator RotateLeft()
    {

        Debug.Log("Rotate Left");


        if (!rotating)
        {
            currentRot = mMenu.transform.eulerAngles;
            rotating = true;  // set the flag
            targetRot.y = currentRot.y + 60; // calculate the new angle

            while (currentRot.y < targetRot.y)
            {
                currentRot.y = Mathf.MoveTowardsAngle(currentRot.y, targetRot.y, DegreesPerSecond * Time.deltaTime);
                mMenu.transform.eulerAngles = currentRot;
                yield return null;
            }

            rotating = false;
        }
    }

    IEnumerator RotateRight()
    {
        Debug.Log("Rotate Right");

        if (!rotating)
        {
            currentRot = mMenu.transform.eulerAngles;
            rotating = true;  // set the flag
            targetRot.y = currentRot.y - 60; // calculate the new angle

            while (currentRot.y > targetRot.y)
            {
                currentRot.y = Mathf.MoveTowardsAngle(currentRot.y, targetRot.y, DegreesPerSecond * Time.deltaTime);
                mMenu.transform.eulerAngles = currentRot;
                yield return null;
            }

            rotating = false;
        }
    }


    void launchNextScene()
    {
        // Load the scene at the given index in build settings.


        SceneManager.LoadScene(mScene);
    }

    private float timeFadeShouldStart()
    {
        return (mSplashDuration - mFadeToBlackDuration);
    }
}
4

0 回答 0