Checking for Screen's Orientation

Hi I am chekcing for a screen’s oriention and am having difficulty in equating it.
Meaning…

if(Screen.orientation == “Portrait”)

returns an error…
Assets/scripts/heroManagerScript.js(153,31): BCE0051: Operator ‘==’ cannot be used with a left hand side of type ‘UnityEngine.ScreenOrientation’ and a right hand side of type ‘String’.

Can anyone tell me how to best check for the devices orientation?

The correct syntax is:

if(Screen.orientation == ScreenOrientation.Portrait)
...
2 Likes

Of course.
thanks.

you are comparing 2 different types of objects. … the system thinks that “Portrait” is a string, and the systems is on the right side :slight_smile:

you should tell unity that the “portrait” is a ScreenOrientation too …so try using this… it should work

if(Screen.orientation == ScreenOrientation.Portrait)

Read up on enums :

http://msdn.microsoft.com/en-us/library/sbbt4032(v=vs.80).aspx

If you get how enums work, you can also check for portrait with

if(Screen.orientation == 0)

(Default underlying value type : integer)

Yes enums,
they seem to be an interesting thing.
Can I declare enums from scratch as in…

enum {go, stay}

where go will automatically = 0 and stay = 1?

Regardless I have the orientation working,
thanks.

Yeah but you have to declare the enum name. It should be more like

enum EnumName {go, stay}

Cool,
thanks.

Well I have spoken too soon.
I am continually getting gdb crashes as a result (more than sure) of my check for screen orientation.
I have done it in two separate ways with the same crash result…

	if(Screen.orientation == ScreenOrientation.Portrait)
	{
		print("P");			
	}
	if(Screen.orientation == ScreenOrientation.PortraitUpsideDown)
	{
		print("PUD");			
	}	
	
	if(Screen.orientation == ScreenOrientation.Landscape)
	{
		print("L");				
	}	
	if(Screen.orientation == ScreenOrientation.LandscapeRight)
	{
		print("LR");				
	}
	if(Screen.orientation == 0)
	{
		print("P");	
		return;		
	}
	if(Screen.orientation == 1)
	{
		print("PUD");	
		return;		
	}	
	if(Screen.orientation == 2)
	{
		print("L");	
		return;		
	}
	if(Screen.orientation == 3)
	{

		print("LR");	
		return;		
	}

This code looks legit. The problem may lie on the code surrounding this part. Post the entire script.

This works…

	if(Screen.orientation == 0)
	{
		print("P");	
		return;		
	}
	if(Screen.orientation == 1)
	{
		print("PUD");	
		return;		
	}

but this doesn’t.
I am thinking, that it is only seeing 2 orientations.

	if(Screen.orientation == 0)
	{
		print("P");	
		return;		
	}
	if(Screen.orientation == 1)
	{	
		print("PUD");	
		return;		
	}	
	if(Screen.orientation == 2)
	{
		print("POO");	
		return;		
	}

going to try some stuff.

oh and full code,
sorry for all the messy garbledee gook.

function OnGUI() {
	print("Screen.orientation " + Screen.orientation);

	
	if(Screen.orientation == 0)
	{
		//var leftBtn = GUI.RepeatButton (Rect (10, 790, 200, 400), leftBtnImage, guiStyle);
		//var rightBtn = GUI.RepeatButton (Rect (420, 790, 200, 400), rightBtnImage, guiStyle);
		//var pauseBtn = GUI.Button(Rect(10, 10, 10, 10), pauseBtnImage, guiStyle);
		print("P");	
		return;		
	}
	if(Screen.orientation == 1)
	{
		//leftBtn = GUI.RepeatButton (Rect (10, 790, 200, 400), leftBtnImage, guiStyle);
		//rightBtn = GUI.RepeatButton (Rect (420, 790, 200, 400), rightBtnImage, guiStyle);
		//pauseBtn = GUI.Button(Rect(10, 10, 10, 10), pauseBtnImage, guiStyle);	
		print("PUD");	
		return;		
	}	
	if(Screen.orientation == 2)
	{
		//leftBtn = GUI.RepeatButton (Rect (10, 790, 200, 400), leftBtnImage, guiStyle);
		//rightBtn = GUI.RepeatButton (Rect (420, 790, 200, 400), rightBtnImage, guiStyle);
		//pauseBtn = GUI.Button(Rect(10, 10, 10, 10), pauseBtnImage, guiStyle);	
		print("POO");	
		return;		
	}				
	
	/*
	if(pauseBtn){
		print("pause game");
		if (Time.timeScale == 1.0){
		btnCommenceData.commenceVar = false; ///var freezes update charctrl movement
        Time.timeScale = 0.0;
        }
        else if(Time.timeScale == 0.0){
        btnCommenceData.commenceVar = true;
        Time.timeScale = 1.0;
        }
    }
    
	if(Time.timeScale == 0.0) ///pause
	return;	
			
	if (leftBtn) {
		print ("you clicked the left");
		if(btnCommenceData.commenceVar == false) /// crnt offical pause var
		return;
		print("btnCommenceData.commenceVar " + btnCommenceData.commenceVar);
		brainCtrlr.Move(leftGravity);
		cameraCtrlr.Move(leftGravity);
		//leftBtn = GUI.RepeatButton (Rect (10, 10, 50, 100), rightBtnImage, guiStyle);
	}
	if (rightBtn) {
		print ("you clicked the right");
		if(btnCommenceData.commenceVar == false) /// crnt offical pause var
		return;	
		print("btnCommenceData.commenceVar " + btnCommenceData.commenceVar);	
		brainCtrlr.Move(rightGravity);
		cameraCtrlr.Move(rightGravity);		
	}	*/
}

Weird,
but this is now working.

	if(Screen.orientation == ScreenOrientation.Portrait)
	{
		print("P");	
		return;		
	}
	if(Screen.orientation == ScreenOrientation.LandscapeLeft)
	{
		print("LSL");	
		return;		
	}	
	
	if(Screen.orientation == ScreenOrientation.LandscapeRight)
	{
		print("LSR");	
		return;		
	}	
		
	
	if(Screen.orientation == ScreenOrientation.PortraitUpsideDown)
	{
		print("PUD");	
		return;		
	}

I wouldn’t use the integer checks. It makes your code harder to read. Unless of course you plan on remembering the index of every single enum you use.

Well the enums are very small in number. 0-3, I think I can work with that.

This is good advice and you shouldn’t be ignoring it.

Being lazy seems easy at the time, but it will come back to bite you later.

1 Like

You can now but what about in 6 months when you revisit the code? Or what if you hire me to write some code for you. I have no idea which is which.

1 Like

Ok no worries,
thank you.