Re-sizing Aspect Ratio Script Not Working

Hello!
I am building a little mobile game and I need some help with the automatic resizing of the resolution for the different screen sizes. I did research and found using a matrix to resize the GUI aspects of the game was best and it works great. My problem comes from, I am guessing, the camera view itself.

I am currently using this script which I thought would fix it but it doesnt:

public class ControllingCameraAspectScript : MonoBehaviour 
{

	void Start()
	{
		// set the desired aspect ratio (the values in this example are
		// hard-coded for 16:9, but you could make them into public
		// variables instead so you can set them at design time)
		float targetaspect = 16.0f / 10.0f;
		
		// determine the game window's current aspect ratio
		float windowaspect = (float)Screen.width / (float)Screen.height;
		
		// current viewport height should be scaled by this amount
		float scaleheight = windowaspect / targetaspect;
		
		// obtain camera component so we can modify its viewport
		Camera camera = GetComponent<Camera>();
		
		// if scaled height is less than current height, add letterbox
		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
		{
			float scalewidth = 1.0f / scaleheight;
			
			Rect rect = camera.rect;
			
			rect.width = scalewidth;
			rect.height = 1.0f;
			rect.x = (1.0f - scalewidth) / 2.0f;
			rect.y = 0;
			
			camera.rect = rect;
		}
	}
}

While it works perfect for my tablet, when I try it on my phone I can actually see different scenes behind my game screen. It’s like my menu scenes are the full screen width, but when I load my main game scene it isn’t as wide/full screen and you can see the edges of my menu scene. So I guess my question is, how can I get my main scene to go full screen? Any idea how to fix this?

I am fairly new with programming so any help will be greatly appreciated!

What’s happening is that when the camera viewport is not taking up the full width of the screen, the part of the screen buffer that it does not draw over is visible. The screen buffer is cleared every frame on some devices and not cleared every frame on other devices, so when it is not cleared, whatever was last drawn to that part of the frame stays there.

There are a few ways to fix this, but the easiest solution is:

  1. Create a new Camera anywhere in the scene (it doesn’t matter)
  2. Set “Clear Flags” to “Solid Color”
  3. Set “Background” to the color you want the letterbox/pillarbox color to be (presumably black)
  4. Set “Culling Mask” to “Nothing”. This ensures no objects will be rendered to this camera; it will only clear the screen color.
  5. Leave the viewport rect as 0,0,1,1 (the full screen)
  6. Set “Depth” to a number lower than the one on the other camera