When I change scene the old scene is still visibile in the background

So I’m changing scene from the Main Menu by doing a simple SceneManager.LoadScene(“Level1”) to load level1 and I’m using a script to fix the camera based on the screen resolution

using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;

public class CameraResFixer : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {
      
        // set the desired aspect ratio, I set it to fit every screen
        float targetaspect = 1920f / 1080f;

        // 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 container box
        {
            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;
        }

    }
    void Update()
    {
      
    }
}

that’s the script I’m using.
After trying it I found out that instead of seeing a black background behind my game background I see the old menu screen, what’s weird is that the old menu disappears when I call the pause menu.
this is before I pause the game link: Imgur: The magic of the Internet
and this is how it looks after calling the pause script, link : Imgur: The magic of the Internet which is how I want it to look

There’s nothing weird in the pause script it’s just:

 void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (gamePaused)
            {
                Resume();
            }
            else
            {
                Pause();
            }
        }
    }

    public void Resume()
    {
        pauseMenu.SetActive(false);
        Time.timeScale = 1;
        gamePaused = false;
        input.ActivateInput();
        Cursor.visible = false;
    }

    public void Pause()
    {
        pauseMenu.SetActive(true);
        Time.timeScale = 0;
        gamePaused = true;
        input.DeactivateInput();
        Cursor.visible = true;

    }

what am I missing? how do I unload or make the old scene background disappear?

Sounds like maybe your next scene doesn’t have a camera that clears all?

Check the camera clear settings in each scene.

1 Like

I’m using URP so my camera doesn’t have the clear flag (atleast that’s what it seems like), I tried setting the game background type to Uninitialized and Solid Color (with a black) but it didn’t work.
Since nothing was working I tried recreating the whole scene and everything was working fine, but of course without the resolution script if playing on low resolutions like 800x600 some elements would become hidden due to the size not scaling, when I tried applying the script again the problem came back, my only guess is that the script is doing something I dont understand.
When I press Escape it pauses the game and a transparent black canvas cover the whole screen to get the blur feeling of a pause menu, my guess is doing that refresh something or clear the buffer so it updates the background and everything that was left from the main menu gets cleared.

I’ll try to see if there’s something I can do in the camera script to clear the background like when I press Escape or maybe find a new way to do it without this script.

I’m still a beginner and I’m doing it as a UNI Project so my knowledge isn’t that good,

After a bit of testing I may just ditch the script entirely and only list resolutions in my setting that have an aspect ratio of 16:9 this way I think unity will handle the sizing just fine.