Game Freezes when switching to different scene. (using Photon Pun 2)

Hello there!

I have a problem when switching scenes.
So, when I click the button to switch scenes my game completely freezes (and does not unfreeze) I have searched everywhere for a solution and tried everything I know about but nothing seems to work.

I hope someone can help :slight_smile:

the code I use for switching scenes (and to spawn the player in) :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.SceneManagement;
using Photon.Pun;
using Unity.VisualScripting;


public class RoomManager : MonoBehaviourPunCallbacks
{
   public static RoomManager Instance;
   

    private void Awake()
    {
        if (Instance)
        {
            Destroy(gameObject);
            return;
        }
        
        Instance = this;
        return;
    }


    private void Start()
    {
        
        PhotonNetwork.AutomaticallySyncScene = true;

    }

    private void Update()
    {
        if (Input.GetKey(KeyCode.P))
        {
            StartGame();
        }
    }


    public override void OnEnable()
    {
        base.OnEnable();
        SceneManager.sceneLoaded += OnSceneLoaded;
        return;
    }


    public override void OnDisable()
    {
        base.OnDisable();
        SceneManager.sceneLoaded -= OnSceneLoaded;
        return;
    }

    

    void OnSceneLoaded(Scene scene, LoadSceneMode loadSceneMode)
    {

        Time.timeScale = 1f;
        if (scene.buildIndex == 1)
        {
            StartCoroutine(SpawnPlayerManager());
            return;
        }
    }


    public void StartGame()
    {
        SceneManager.LoadScene("MainMaze");
    }

    IEnumerator SpawnPlayerManager()
    {
        yield return new WaitForSeconds(3);
        PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "PlayerManager"), Vector3.zero, Quaternion.identity);
    }





}

I fixed it!

The only thing I needed to do was adding the same script to the scene you want to load to and then just change the if (scene.buildIndex == 1) to if (scene.buildIndex == 2) I did it with a float so I didn’t have to make a whole new script :slight_smile: