Need some help with Camera

I’m making a 2D game that auto-resizes some of the game elements like the foreground and background depending on the resolution of the screen. I am doing so with this Pixel Perfect Camera. I need help because I am trying to switch scenes and whenever I switch out of this scene and switch back into it, the camera zooms again and messes up my entire game. I guess I need to find a way to reset the camera so that when I come back into the game it can resize itself. I’m sure there is a better way to do this, but I can’t think of it so any and all feedback is appreciated!! Thank you!

using UnityEngine;
using System.Collections;

public class PixelPerfectCamera : MonoBehaviour {

public static float pixelsToUnits = 1f;
public static float scale = 1f;

public Vector2 nativeResolution = new Vector2 (240, 160);

void Awake () {
var camera = GetComponent ();

if (camera.orthographic) {
scale = Screen.height/nativeResolution.y;
pixelsToUnits *= scale;
camera.orthographicSize = (Screen.height / 2.0f) / pixelsToUnits;
}
}

public void Reset(){

}
}

*Sorry if I messed something up this is my first post.

2195722–145721–PixelPerfectCamera.cs (473 Bytes)

public static float pixelsToUnits = 1f;
public static float scale = 1f;

So you’ve got pixelsToUnits and scale as static variables, meaning those variable will never go away. When you set them once, they will stay that way until you set them again, even if the script is attached to a different game object or you change scenes, etc. (The only time the static variable will get set back to it’s original value is when you restart the whole game.)

To give you an example, lets say your scale is 2. The first time you make your camera, your pixelsToUnits will be 1, then you multiply it by the scale to get 2. Now you’ve switched scenes and switched back, but your pixelsToUnits is still 2, so when you multiply it by the scale, pixelsToUnits is now 4, which is incorrect!

You can solve this problem by making those variable not static, by only setting them once, or by reseting the values before you recalculate.

Hope this helps!