Im new to Unity and C, this issue is driving me mad and i feel im missing the obvious,
Basically i have a gameobject which i need to rotate on Swipe left or Right (GearVR)
When the scene initially loads everything is fine. When i load a new scene and then re load the Main Menu scene the startcoroutine cause a NullReferenceException error.
The whole thing works perfectly in the editor, only when on the Android device do i get the error.
Ive read everything i can find but i dont understand how the StartCoroutine is throwing the error.
Please help
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);
}
}