Reputation: 7395
I am creating a VR app using Google Cardboard in Unity and I have succeeded in switching from VR to a normal view with this function:
public GameObject[] cardboardObjects;
public GameObject[] monoObjects;
public bool switched = false;
// Turn on or off VR mode
void ActivateVRMode(bool goToVR) {
foreach (GameObject cardboardThing in cardboardObjects) {
cardboardThing.SetActive(goToVR);
}
foreach (GameObject monoThing in monoObjects) {
monoThing.SetActive(!goToVR);
}
Cardboard.SDK.VRModeEnabled = goToVR;
// Tell the game over screen to redisplay itself if necessary
//gameObject.GetComponent<GameController>().RefreshGameOver();
}
I then call this function on a button to switch to mono view:
public void Switch() {
ActivateVRMode(false);
switched = true;
Debug.Log (switched+"No VR!");
}
I then want to have the mono view on a timer, as in once the user switches to non-VR mode it remains in that for only a set amount of time (3 seconds or so) before switching back to VR.
I tried to do this by flipping a boolean and using WaitForSeconds
, however the view remains in mono view and nothing happens:
IEnumerator Switchback()
{
yield return new WaitForSeconds(3);
ActivateVRMode(true);
switched = false;
Debug.Log (switched+"VR!");
}
void Update () {
if (switched == true) {
Switchback ();
}
//Debug.Log ("updating");
}
What am I doing wrong here?
Upvotes: 0
Views: 188
Reputation: 2031
You have forgotten to call the Switchback
function as a Coroutine
Instead of calling Switchback()
you need to call StartCoroutine(Switchback())
in your Update()
function.
Upvotes: 3