# Unity 5 - Problem changing camera rect viewport on runtime

**URL:** <https://discussions.unity.com/t/unity-5-problem-changing-camera-rect-viewport-on-runtime/601913>\
**Category:** Mobile\
**Tags:** Android\
**Created:** [October 18, 2015, 6:37am UTC](https://discussions.unity.com/t/unity-5-problem-changing-camera-rect-viewport-on-runtime/601913 "2015-10-18T06:37:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![notoi\_74](https://avatars.discourse-cdn.com/v4/letter/n/3ec8ea/32.png) [@notoi\_74](https://discussions.unity.com/u/notoi_74)\
**Post date:** [October 18, 2015, 6:37am UTC](https://discussions.unity.com/t/unity-5-problem-changing-camera-rect-viewport-on-runtime/601913/1 "2015-10-18T06:37:29Z")

</div>

Hello guys, I’m doing my first Unity game and I want user can play it in portrait and landscape. The game have a UI/canvas area that with a script I turn on or off. It works well. And the game area that It’s 3d graphics. With another script I modify the aspect ratio of the game changing the viewport rect of the camera. Something like this:

…  
if (scaleheight \< 1.0f)  
{  
Rect rect = camera.rect;

rect.width = 1.0f;  
rect.height = scaleheight;  
rect.x = 0;  
rect.y = (1.0f - scaleheight) / 2.0f;

camera.rect = rect;  
}  
else // add pillarbox  
{ … }

This script is called at start to change to the right camera aspect and It’s called too when you change from portrait to landscape or landscape to portrait with the android device.

In Unity editor it works well. I select portrait or landscape and play the game and It works. In my Android device It doen’t work well. Only It works when I rotate the android device and the view changes. But If I have my Android device in my hands in portrait orientation and I play the game, the script has no effect. Any idea?

---

<div class="post-metadata">

**Author:** ![notoi\_74](https://avatars.discourse-cdn.com/v4/letter/n/3ec8ea/32.png) [@notoi\_74](https://discussions.unity.com/u/notoi_74)\
**Post date:** [October 20, 2015, 6:50pm UTC](https://discussions.unity.com/t/unity-5-problem-changing-camera-rect-viewport-on-runtime/601913/2 "2015-10-20T18:50:53Z")

</div>

Hi! I found what It was happening. I have at the moment two scenes. The first scene is where I will put some splash screen and then this will call to load the second scene what is the game and where I was testing camera viewport rect. At the moment at the first scene there’s a default unity scene with his camera and a sphere. I put in this scene a script with Aplication.LoadLevel(“scene2”) to load inmediately the second scene(the game) that I was testing camera. It seems that I have to put the camera viewport script in the first scene because if it has no script when I load the second scene and run the script It has no effect. Only It works when I rotate the device. But If in the first scene I put the viewport rect script It will work properly and when load the second scene I will see with the right aspect ratio. Maybe is some bug on Unity? I don’t know.
