- Home /
VRSettings.enabled sometimes not working.
Hi,
I'm having a bit of a confusion with toggling VR on and off in my project.
I have the following public function that toggles the VR mode on or off, depending on the user's action (i.e. clicking on a game object).
public void toggleVR()
{
isVROn = !isVROn;
if (isVROn)
{
Screen.orientation = ScreenOrientation.LandscapeLeft;
StartCoroutine(LoadVRDevice("Cardboard",isVROn));
}
else
{
Screen.orientation = ScreenOrientation.Portrait;
StartCoroutine(LoadVRDevice("None", isVROn));
}
}
and here is the coroutine that is called
private IEnumerator LoadVRDevice (string device, bool enabled)
{
VRSettings.LoadDeviceByName(device);
yield return null;
if (!VRSettings.loadedDeviceName.Equals(device))
{
Debug.Log("Waiting extra frame");
yield return null;
}
Debug.Log("Loaded device:" + VRSettings.loadedDeviceName);
yield return null;
VRSettings.enabled = enabled;
}
This two functions work pretty alright, but I've encountered issues where the console will throw the following message when I attempt to enable VR:
OPENGL NATIVE PLUG-IN ERROR: GL_INVALID_OPERATION: Operation illegal in current stateUnityEngine.VR.VRSettings:set_enabled(Boolean)
It usually happens after I add some function/coroutine calls to other scripts which are called much earlier than the toggleVR method, and when it happens, my reticle no longer respond to objects.
So, my question is more to, what are the common causes for this error? Is my coroutine not working, or is it something else? Would really like to know what's going on cause it's been bugging me for days now :(
Just a heads up that am pretty new to Unity and VR, so what I've written may not be the best approach, but it's what I've gotten from searching around online and all.
Thanks in advance.
I could not undestand why do you placed these yield returns in your coroutine. I mean, the only yield return needed in my opinion is the one after LoadDeviceByName(), since the device will only be called at the beginning of the next frame. The other calls seems pretty useless to me. Could you explain me that?
Well, try to simplify your LoadDeviceVR method to something like this:
private IEnumerator LoadVRDevice (string device, bool enabled){
VRSettings.LoadDeviceByName(device);
yield return null;
VRSettings.enabled = enabled;
}
Tell me what happened.
Hi Liinnkk,
Well, I thought maybe there's a delay in loading the device, hence I added in the yields. I've not fully grasped the full understanding of yields but from reading around online, that's what I came up with, redundant maybe, but it kinda worked at that time.
Also, I tried simplifying the method as below:
private IEnumerator LoadVRDevice (string device, bool enabled)
{
VRSettings.LoadDeviceByName(device);
yield return null;
//I kept the debug here to see if it loads. Debug.Log("Loaded device:" + VRSettings.loadedDeviceName); VRSettings.enabled = enabled; }
but still no success :( What are the usual causes for this OpenGL error though? I don't quite get it. :(
Also, thanks for the response.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity Arduino VR problem 1 Answer
Google Daydream Thread with Unity 1 Answer