IOS orientation loss on scene change

I have a menu scene that remains in landscape and another scene accessed from the menu scene using application.loadlevel() which can be all orientations.

I have attached the script below to a game object in both scenes with the flag set for landscape only for the menu and not set for the other scene and this works great on app start.

However if when in the second scene I am in portrait and return to the menu scene it displays the menu incorrectly as it remains in portrait rather than on menu load resetting to landscape only.

Am I doing this the wrong way?

Thanks

public class DeviceOrientation : MonoBehaviour {

	public bool landscapeOnly;

	void Awake () {
	
		if (landscapeOnly) {

			// scene remains in landscape only

			Screen.orientation = ScreenOrientation.AutoRotation;
			Screen.autorotateToLandscapeLeft = true;
			Screen.autorotateToLandscapeRight = true;
			Screen.autorotateToPortrait = false;
			Screen.autorotateToPortraitUpsideDown = false;
		} 
		else {

			// scene allows auto rotation in all directions

			Screen.orientation = ScreenOrientation.AutoRotation;
			Screen.autorotateToLandscapeLeft = true;
			Screen.autorotateToLandscapeRight = true;
			Screen.autorotateToPortrait = true;
			Screen.autorotateToPortraitUpsideDown = true;
		}
	}
}

Hi elpuerco63!

Perhaps you could do so: this.DontDestroyOnLoad()

(you could change “this” with gameObject or whatever you want! “This” refers to the actual script)

(Read about it: http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html)

When a new scene is loaded, this script won’t be destroyed and recreated.

Bests, Math

This is from memory, from a “good enough” solution similar to this a year ago:

Setting orientation to AutoRotate only keys off of movement. It won’t spin the screen now. I think you need to set orientation to a specific value, in order to force a change. Of course, then you need to set it back to autorotate, to enable the 180 flip.

Normally you don’t have to worry about this, since the build settings force orientation to be legal on start-up, and most people never change autorotate. But, I think, for example, if you allowed any rotation in your build, and started the game end-up, the menu would be messed-up right away.